import sqlite3
# 连接到SQLite数据库
# 数据库文件是mrsoft.db,如果文件不存在,会自动在当前目录创建
# 在本python所在文件目录下生成了一个mrsoft.db文件
'''
如何打开SQLite format 3 数据库文件?
https://jingyan.baidu.com/article/e8cdb32b4343e637052bad0f.html
'''
conn = sqlite3.connect('mrsoft.db')
# 创建一个Cursor
cursor = conn.cursor()
# 执行一条SQL语句,创建user表
cursor.execute('create table test (id int(10) primary key, name varchar(20))')
cursor.execute('insert into test (id,name) values ("1","MRSOFT")')
cursor.execute('insert into test (id,name) values ("2","Andy")')
cursor.execute('insert into test (id,name) values ("3","小助手")')
cursor.execute('update test set name = ? where id = ?',('MR',1))
cursor.execute('delete from test where id = ?',(1,))
# 执行查询语句
cursor.execute('select * from test')
# 获取查询结果
result1 = cursor.fetchone()
print(result1)
result2 = cursor.fetchmany(2) # 使用fetchmany方法查询多条数据
print(result2)
result3 = cursor.fetchall() # 使用fetchmany方法查询多条数据
print(result3)
cursor.execute('select * from test where id > ?',(1,))
result3 = cursor.fetchall()
print(result3)
# 关闭游标
cursor.close()
# 关闭Connection
conn.close()