当前位置:Gxlcms > Python > python操作sqlite的CRUD实例分析

python操作sqlite的CRUD实例分析

时间:2021-07-01 10:21:17 帮助过:65人阅读

本文实例讲述了python操作sqlite的CRUD实现方法。分享给大家供大家参考。具体如下:

  1. import sqlite3 as db
  2. conn = db.connect('mytest.db')
  3. cursor = conn.cursor()
  4. cursor.execute("drop table if exists datecounts")
  5. cursor.execute("create table datecounts(date text, count int)")
  6. cursor.execute('insert into datecounts values("12/1/2011",35)')
  7. cursor.execute('insert into datecounts values("12/2/2011",42)')
  8. cursor.execute('insert into datecounts values("12/3/2011",38)')
  9. cursor.execute('insert into datecounts values("12/4/2011",41)')
  10. cursor.execute('insert into datecounts values("12/5/2011",40)')
  11. cursor.execute('insert into datecounts values("12/6/2011",28)')
  12. cursor.execute('insert into datecounts values("12/7/2011",45)')
  13. conn.row_factory = db.Row
  14. cursor.execute("select * from datecounts")
  15. rows = cursor.fetchall()
  16. for row in rows:
  17. print("%s %s" % (row[0], row[1]))
  18. cursor.execute("select avg(count) from datecounts")
  19. row = cursor.fetchone()
  20. print("The average count for the week was %s" % row[0])
  21. cursor.execute("delete from datecounts where count = 40")
  22. cursor.execute("select * from datecounts")
  23. rows = cursor.fetchall()
  24. for row in rows:
  25. print("%s %s" % (row[0], row[1]))

希望本文所述对大家的Python程序设计有所帮助。

人气教程排行