python操作sqlite的CRUD
python操作sqlite的CRUD,本范例创立了一个sqlite数据库,然后演示了插入、查询和统计数据的各种方法
""" 皮蛋编程(https://www.pidancode.com) 创建日期:2022/3/29 功能描述:python操作sqlite的CRUD """ import sqlite3 as db conn = db.connect('mytest.db') cursor = conn.cursor() cursor.execute("drop table if exists datecounts") cursor.execute("create table datecounts(date text, count int)") cursor.execute('insert into datecounts values("12/1/2022",35)') cursor.execute('insert into datecounts values("12/2/2022",42)') cursor.execute('insert into datecounts values("12/3/2022",38)') cursor.execute('insert into datecounts values("12/4/2022",41)') cursor.execute('insert into datecounts values("12/5/2022",40)') cursor.execute('insert into datecounts values("12/6/2022",28)') cursor.execute('insert into datecounts values("12/7/2022",45)') conn.row_factory = db.Row cursor.execute("select * from datecounts") rows = cursor.fetchall() for row in rows: print("%s %s" % (row[0], row[1])) cursor.execute("select avg(count) from datecounts") row = cursor.fetchone() print("The average count for the week was %s" % row[0]) cursor.execute("delete from datecounts where count = 40") cursor.execute("select * from datecounts") rows = cursor.fetchall() for row in rows: print("%s %s" % (row[0], row[1]))
相关文章