数据库批量插入数据
批量插入时一直报错,后来发现是占位符后面忘记写分号了
#向数据库插入信息 import pymysql,random FI = pymysql.connect( host='192.168.0.12',user='root',passwd='myjcyf', port=3306,db='us_sys',charset='utf8' ) cur = FI.cursor() #建立游标 for i in range(20): bh = str(random.randint(0,9999)) nbh=bh.zfill(4) name1='zhbr_'+nbh sex=random.randint(0,1) sql = "insert INTO us_sys.stu (id,name,sex) VALUE ('%s','%s','%s');"%(nbh,name1,sex) cur.execute(sql) FI.commit() cur.close() FI.close()
从数据库删除信息
#从数据库删除信息 # import pymysql # FI = pymysql.connect( # host='192.168.0.12',user='root',passwd='myjcyf', # port=3306,db='us_sys',charset='utf8' # ) # cur = FI.cursor() #建立游标 # sql = "delete from us_sys.stu " # cur.execute(sql) # FI.commit() # cur.close() # FI.close()