利用Python代向像数据库中添加多天数据(主要用来造数据)
def insertData(db,cursor,tablename,*key,**kwargs): # *key返回的是元组(),**返回的是字典 values = [] for value in kwargs.values(): values.append(value) print(tuple(values)) sql = ‘insert into {} {}‘.format(tablename,key).replace("‘","")+‘ VALUES {}‘.format(tuple(values)) print(sql) try: cursor.execute(sql) db.commit() print("成功添加数据") print("插入数据的ID:",cursor.lastrowid) except Exception as e: print(e) # 发生错误时候回滚 db.rollback()
db = config.db cur = db.cursor(cursor=cursors.DictCursor) for i in range(0,99): name = ‘wangquntest_‘.join(random.sample(string.ascii_letters+string.digits+string.punctuation,3)) age =random.randint(20,99) insertData(db,cur,"ty_test_info","name","age",name = name,age = age)
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30