python操作sqlite的CRUD实例分析

yipeiwu_com5年前Python基础

本文实例讲述了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/2011",35)')
cursor.execute('insert into datecounts values("12/2/2011",42)')
cursor.execute('insert into datecounts values("12/3/2011",38)')
cursor.execute('insert into datecounts values("12/4/2011",41)')
cursor.execute('insert into datecounts values("12/5/2011",40)')
cursor.execute('insert into datecounts values("12/6/2011",28)')
cursor.execute('insert into datecounts values("12/7/2011",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]))

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

相关文章

详解python之协程gevent模块

Gevent官网文档地址:http://www.gevent.org/contents.html 进程、线程、协程区分 我们通常所说的协程Coroutine其实是corporate ro...

Python机器学习之scikit-learn库中KNN算法的封装与使用方法

Python机器学习之scikit-learn库中KNN算法的封装与使用方法

本文实例讲述了Python机器学习之scikit-learn库中KNN算法的封装与使用方法。分享给大家供大家参考,具体如下: 1、工具准备,python环境,pycharm 2、在机器学...

Python线程障碍对象Barrier原理详解

python线程Barrier俗称障碍对象,也称栅栏,也叫屏障。 一.线程障碍对象Barrier简介 # 导入线程模块 import threading # 障碍对象barrier...

利用Python实现颜色色值转换的小工具

利用Python实现颜色色值转换的小工具

先看看Zeplin 的颜色色值显示示例 原有处理方式 因为我会 Python (仅限于终端输入 python 然后当做计算器算,或者用 hex() 函数把十进制转换成十六进制),所...

Python实现对特定列表进行从小到大排序操作示例

本文实例讲述了Python实现对特定列表进行从小到大排序操作。分享给大家供大家参考,具体如下: 1、在系统内新建文件rizhireplacelist.txt root@kali:~#...