Python SQLite3使用

来源:互联网 发布:网页铃声制作软件 编辑:程序博客网 时间:2024/05/29 16:17
import sqlite3conn = 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()

运行结果

这里写图片描述