python 连接sqlite及简单操作
废话不多说了,直接给大家贴代码了,具体代码如下所示:
importsqlite3
#查询
defload(table):
#连接数据库
con=sqlite3.connect("E:/Datebase/SQLiteStudio/Park.db")
#获得游标
cur=con.cursor()
#查询整个表
cur.execute('select*from'+table)
lists=['name','password']
iftable=='login':
#将数据库列名存入字典
colnames={desc[0]fordescincur.description}
将字典和数据库的数据一起存入列表,获得了记录字典
rowdicts=[dict(zip(lists,row))forrowincur.fetchall()]
else:
rowdicts=[]
forrowincur:
rowdicts.append(row)
con.commit()
cur.close()
returnrowdicts
#插入数据
definsert_data(ID,name,money):
con=sqlite3.connect("E:/Datebase/SQLiteStudio/Park.db")
cur=con.cursor()
#使用SQL语句插入
cur.execute('insertintoChargevalues(?,?,?)',(ID,name,money))
#插入后进行整表查询,看是否成功插入
cur.execute('select*fromCharge')
print(cur.fetchall())
con.commit()
cur.close()
以上所述是小编给大家介绍的python连接sqlite及简单操作,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!