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()
###运行结果