Python SQLite3使用

import sqlite3

conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('''create table people
	(id int primary key not null,
	name text not null,
	age int not null)''')
c.execute("insert into people values(12341,'周驰',23)")
c.execute("insert into people values(12522,'李名',22)")
c.execute("insert into people values(12309,'大米',33)")
c.execute("insert into people values(12301,'张红',13)")
c.execute("insert into people values(?, ?, ?)", (12311,'张名',12))
#fetch 取来
cursor = c.execute('select * from people')
print(cursor.fetchone()) 
#从查询中取一行,取一行tuple类型
print(cursor.fetchmany(3)) 
#从查询中取三行,为List类型 
cursor2 = cursor.fetchall()
#从查询中取剩余,List类型,虽然只有一行
print('cursor:',cursor)  #sqlite3.Cursor类型
print('cursor2:',cursor2)  #list类型

for row in cursor:
	print(row[1],'  ******') #光标已经滑到最后,cousor中已无数据
print()
conn.commit()
c.execute("delete from people where name = '周驰'")
cursor = c.execute('select * from people')
for row in cursor:
	print(row[1])
print()
c.execute("delete from people where name = '大米'")
conn.rollback()
print('回滚到上一次调用commit()时的数据库状态\n')
cursor = c.execute('select * from people')
for row in cursor:
	print(row[1])
conn.close()

###运行结果
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值