sqlite3代码片段

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()















































 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值