python操作sqlite的CRUD实例分析


Posted in Python onMay 08, 2015

本文实例讲述了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 相关文章推荐
python中遍历文件的3个方法
Sep 02 Python
C#返回当前系统所有可用驱动器符号的方法
Apr 18 Python
Python实现抓取HTML网页并以PDF文件形式保存的方法
May 08 Python
Python实现KNN(K-近邻)算法的示例代码
Mar 05 Python
django中ORM模型常用的字段的使用方法
Mar 05 Python
Tensorflow 使用pb文件保存(恢复)模型计算图和参数实例详解
Feb 11 Python
Python图像处理库PIL的ImageGrab模块介绍详解
Feb 26 Python
在matplotlib中改变figure的布局和大小实例
Apr 23 Python
实例讲解Python 迭代器与生成器
Jul 08 Python
Selenium Webdriver元素定位的八种常用方式(小结)
Jan 13 Python
Django 如何实现文件上传下载
Apr 08 Python
Python学习之os包使用教程详解
Mar 21 Python
python实现在sqlite动态创建表的方法
May 08 #Python
python查询sqlite数据表的方法
May 08 #Python
python自定义类并使用的方法
May 07 #Python
python简单的函数定义和用法实例
May 07 #Python
python中for语句简单遍历数据的方法
May 07 #Python
python中while循环语句用法简单实例
May 07 #Python
python使用range函数计算一组数和的方法
May 07 #Python
You might like
php制作中间带自己定义图片二维码的方法
2014/01/27 PHP
Yii2 queue的队列使用详解
2019/07/19 PHP
JavaScript 获取事件对象的注意点
2009/07/29 Javascript
40个新鲜出炉的jQuery 插件和免费教程[上]
2012/07/24 Javascript
javascript实现炫酷的拖动分页
2015/05/11 Javascript
js实现滚动条滚动到某个位置便自动定位某个tr
2021/01/20 Javascript
JavaScript字符串常用的方法
2016/03/10 Javascript
JavaScript对象数组排序实例方法浅析
2016/06/15 Javascript
深入理解Javascript中的valueOf与toString
2017/01/04 Javascript
angular forEach方法遍历源码解读
2017/01/25 Javascript
Vue的移动端多图上传插件vue-easy-uploader的示例代码
2017/11/27 Javascript
基于JavaScript实现幸运抽奖页面
2020/07/05 Javascript
js实现json数组分组合并操作示例
2019/02/12 Javascript
基于mpvue小程序使用echarts画折线图的方法示例
2019/04/24 Javascript
Vue 刷新当前路由的实现代码
2019/09/26 Javascript
[02:16]DOTA2英雄基础教程 干扰者
2014/01/15 DOTA
[09:13]2014DOTA2国际邀请赛 中国区预选赛coser表演
2014/05/23 DOTA
[00:17]天涯墨客一技能展示
2018/08/25 DOTA
python中List的sort方法指南
2014/09/01 Python
Python判断文本中消息重复次数的方法
2016/04/27 Python
Python简单获取二维数组行列数的方法示例
2018/12/21 Python
对python中的控制条件、循环和跳出详解
2019/06/24 Python
python根据文本生成词云图代码实例
2019/11/15 Python
上班早退检讨书
2014/01/09 职场文书
《尊严》教学反思
2014/02/11 职场文书
农村改厕实施方案
2014/03/22 职场文书
三好学生个人先进事迹材料
2014/05/17 职场文书
环境日宣传活动总结
2014/07/09 职场文书
自我检讨书范文
2015/01/28 职场文书
质量保证书怎么写
2015/02/27 职场文书
幼儿园园长个人总结
2015/03/02 职场文书
2015年教师教学工作总结
2015/04/28 职场文书
2016年公司新年寄语
2015/08/17 职场文书
职场新人刚入职工作总结该怎么写?
2019/05/15 职场文书
创业的9条正确思考方式
2019/08/26 职场文书
CSS实现两列布局的N种方法
2021/08/02 HTML / CSS