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程序设计有所帮助。

相关文章

深入理解Python3中的http.client模块

http 模块简介 Python3 中的 http 包中含有几个用来开发 HTTP 协议的模块。 http.client 是一个底层的 HTTP 协议客户端,被更高层的 urlli...

python 3.6 tkinter+urllib+json实现火车车次信息查询功能

python 3.6 tkinter+urllib+json实现火车车次信息查询功能

一、概述 妹子工作时需要大量地查询火车车次至南京的信息,包括该车次到达站(南京站or南京南站)、到达时间、出发时间等,然后根据这些信息做下一步工作。 版本结束,趁着间歇期,帮她弄了个简易...

用Python读取几十万行文本数据

我在使用python读取几十万行的文件中的数据,并构造字典,列表等数据结构时,再访问字典,列表时,一般都会出现内存不够的问题,然后只能循环读取几百行或者一定数量的行数来循环操作。 k...

详解用Python为直方图绘制拟合曲线的两种方法

详解用Python为直方图绘制拟合曲线的两种方法

直方图是用于展示数据的分组分布状态的一种图形,用矩形的宽度和高度表示频数分布,通过直方图,用户可以很直观的看出数据分布的形状、中心位置以及数据的离散程度等。 在python中一般采用m...

基于torch.where和布尔索引的速度比较

我就废话不多说了,直接上代码吧! import torch import time x = torch.Tensor([[1, 2, 3], [5, 5, 5], [7, 8, 9]...