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基础教程之自定义函数介绍
Aug 29 Python
跟老齐学Python之编写类之四再论继承
Oct 11 Python
Python实用日期时间处理方法汇总
May 09 Python
Python cookbook(数据结构与算法)将名称映射到序列元素中的方法
Mar 22 Python
python基于http下载视频或音频
Jun 20 Python
创建Django项目图文实例详解
Jun 06 Python
Python turtle绘画象棋棋盘
Aug 21 Python
在 Linux/Mac 下为Python函数添加超时时间的方法
Feb 20 Python
500行python代码实现飞机大战
Apr 24 Python
在django admin中配置搜索域是一个外键时的处理方法
May 20 Python
python集合的新增元素方法整理
Dec 07 Python
python神经网络学习 使用Keras进行回归运算
May 04 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的五种设计模式
2012/09/05 PHP
php中把美国时间转为北京时间的自定义函数分享
2014/07/28 PHP
Laravel框架路由配置总结、设置技巧大全
2014/09/03 PHP
php绘图之生成饼状图的方法
2015/01/24 PHP
php获取当前页面完整URL地址
2015/12/30 PHP
thinkPHP2.1自定义标签库的导入方法详解
2016/07/20 PHP
php 生成加密公钥加密私钥实例详解
2017/06/16 PHP
php中目录操作opendir()、readdir()及scandir()用法示例
2019/06/08 PHP
filemanage功能中用到的lib.js
2007/04/08 Javascript
IE iframe的onload方法分析小结
2010/01/07 Javascript
JavaScript面向对象之静态与非静态类
2010/02/03 Javascript
13 个JavaScript 性能提升技巧分享
2012/07/26 Javascript
jQuery+css+html实现页面遮罩弹出框
2013/03/21 Javascript
angularjs基础教程
2014/12/25 Javascript
JavaScript学习笔记之Cookie对象
2015/01/22 Javascript
JavaScript三元运算符的多种使用技巧
2015/04/16 Javascript
js实现每日自动换一张图片的方法
2015/05/04 Javascript
JS实现点击按钮获取页面高度的方法
2015/11/02 Javascript
jquery延迟对象解析
2016/10/26 Javascript
pm2 部署 node的三种方法示例
2017/10/20 Javascript
angularjs 页面自适应高度的方法
2018/01/17 Javascript
[55:56]NB vs Infamous 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.22
2019/09/05 DOTA
Python with用法实例
2015/04/14 Python
python文件及目录操作代码汇总
2020/07/08 Python
The North Face北面德国官网:美国著名户外品牌
2018/12/12 全球购物
MyHeritage美国:家族史研究和DNA测试的领先服务
2019/05/27 全球购物
美国轻奢时尚购物网站:REVOLVE(支持中文)
2020/07/18 全球购物
超市总经理岗位职责
2014/02/02 职场文书
采购部经理岗位职责
2014/02/10 职场文书
幼儿教师研修感言
2014/02/12 职场文书
中国入世承诺
2014/04/01 职场文书
教师节寄语2015
2015/03/23 职场文书
教师节主题班会方案
2015/08/17 职场文书
校运会班级霸气口号
2015/12/24 职场文书
简历自我评价:教师师德表现自我评价
2019/04/24 职场文书
导游词之香港-太平山顶
2019/10/18 职场文书