sqlite增删改查
import sqlite3
#如果文件不存在会自动创建
conn=sqlite3.connect('test.db')
#创建一个cursor
cursor=conn.cursor()
cursor.execute('DROP TABLE user')
#执行一条sql:create表
cursor.execute('create table user(id varchar(20) primary key,name varchar(100))')
#插入一条语句
cursor.execute('insert into user(id,name) values (\'1\', \'Michael\')')
#获取插入的行数
print(cursor.rowcount)
#关闭cursor
cursor.close()
#提交事务
conn.commit()
conn.close()
#查询记录
conn = sqlite3.connect('test.db')
cursor = conn.cursor()
cursor.execute('select * from user where id=?', ('1',))
values = cursor.fetchall()
#获取查询结果
print(values)
cursor.execute("update user set name ='shopify' where id=1")
#修改并提交数据
conn.commit()
cursor.execute("select * from user " )
values = cursor.fetchall()
print(values)
#提交事务
conn.close()
标签:Sqlite,改查,execute,cursor,values,user,增删,id,conn
From: https://www.cnblogs.com/xfbk/p/16705761.html