python操作sqlite的CRUD实例分析

yipeiwu_com6年前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中如何使用insert函数

这篇文章主要介绍了python中如何使用insert函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 描述 insert() 函数用...

使用python将mysql数据库的数据转换为json数据的方法

使用python将mysql数据库的数据转换为json数据的方法

由于产品运营部需要采用第三方个推平台,来推送消息。如果手动一个个键入字段和字段值,容易出错,且非常繁琐,需要将mysql的数据转换为json数据,直接复制即可。 本文将涉及到如何使用Py...

Python中的连接符(+、+=)示例详解

前言 本文通过在一段示例代码中发现的问题,来给大家详细介绍了Python中的连接符(+、+=),下面话不多说,来看详细的介绍吧。 假设有下面一段代码: a = [1, 2, 3, 4...

python自动化脚本安装指定版本python环境详解

python自动化脚本安装指定版本python环境详解

一般情况下编译安装python环境需要执行以下步骤: 下载源码包 解压源码包 安装配置 编译以及编译安装 TALK IS CHEAP, SHOW YOU MY...

Windows 8.1 64bit下搭建 Scrapy 0.22 环境

Windows 8.1 64bit下搭建 Scrapy 0.22 环境

我的Windows 8.1 环境 1.下载安装Python 2.7.6 在Python官方网站中下载Python2.7.6的Windows安装包,根据默认配置安装到C:\Python2...