注意:
1、切记一定要commit() ,否则存不到数据库
2、可以多条execute() 然后一次性commit()
3、使用完后记得close()
import sqlite3 def saveDataToDB(data): connect = sqlite3.connect("test.db") c = connect.cursor() #获取游标 # i=1 # for title in data: # sql = r"insert into t1 (id,title) values (%d,'%s');"%(i,title) # i+=1 # print(sql) # c.execute(sql) titleList = data['titleList'] detailList = data['detailList'] for key,value in titleList.items(): sql = r"insert into title (id,title) values (%d,'%s');" %(key, value) c.execute(sql) for val in detailList: sql = r"insert into detail (`name`,titleId,url) values ('%s',%d,'%s');" %(val[1], val[0],val[2]) c.execute(sql) connect.commit() c.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)