Python简单操作sqlite3的方法示例


Posted in Python onMarch 22, 2017

本文实例讲述了Python简单操作sqlite3的方法。分享给大家供大家参考,具体如下:

import sqlite3
def Test1():
  #con =sqlite3.connect("D:\\test.db")
  con =sqlite3.connect(":memory:") #store in memory
  cur =con.cursor()
  try:
    cur.execute('create table score(id integer primary key,name varchar(10),scores integer)')
    cur.execute("insert into score values(0,'Rose',87)")
    cur.execute("insert into score values(1,'Alice',78)")
    cur.execute("insert into score values(2,'Helon',100)")
    cur.execute("insert into score values(3,'Tom',98)")
    cur.execute("insert into score values(4,'jack',198)")
    cur.execute("insert into score values(5,'Tony',198)")
    cur.execute("insert into score values(6,'David',99)")
    cur.execute("update score set scores =? where id=?",(45,3))
    cur.execute("update score set name=? where id=?",("John",0))
    cur.execute("delete from score where id =1")
  except Exception,e:
    print "There are some except",e
  con.commit()
  print "Insert Complete"
  print "-----------------------------------------"
  print "Last row id is ",cur.lastrowid
  cur.execute('select * from score')
  print cur.fetchall()
  print "----------------------------------------"
  cur.execute("select count(*) from score")
  print "Current Rows is :",cur.fetchall()[0]
  cur.close()
  con.close()
if __name__ =='__main__':
  Test1()
  print "hello world"

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

Python 相关文章推荐
python实现类似ftp传输文件的网络程序示例
Apr 08 Python
Python二维码生成库qrcode安装和使用示例
Dec 16 Python
python实现同时给多个变量赋值的方法
Apr 30 Python
200行自定义python异步非阻塞Web框架
Mar 15 Python
遗传算法之Python实现代码
Oct 10 Python
解决Python pandas plot输出图形中显示中文乱码问题
Dec 12 Python
python对csv文件追加写入列的方法
Aug 01 Python
Python:slice与indices的用法
Nov 25 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
Dec 10 Python
python中with语句结合上下文管理器操作详解
Dec 19 Python
Python实现获取当前目录下文件名代码详解
Mar 10 Python
Python .py生成.pyd文件并打包.exe 的注意事项说明
Mar 04 Python
Python创建xml文件示例
Mar 22 #Python
用Python将IP地址在整型和字符串之间轻松转换
Mar 22 #Python
用python写一个windows下的定时关机脚本(推荐)
Mar 21 #Python
利用Python实现Windows定时关机功能
Mar 21 #Python
使用Python生成XML的方法实例
Mar 21 #Python
Python存取XML的常见方法实例分析
Mar 21 #Python
Python正则简单实例分析
Mar 21 #Python
You might like
保存到桌面、设为桌面且带图标的PHP代码
2013/11/19 PHP
CI框架自动加载session出现报错的解决办法
2014/06/17 PHP
yii2简单使用less代替css示例
2017/03/10 PHP
jquery ui dialog ie8出现滚动条的解决方法
2010/12/06 Javascript
基于jquery的$.ajax async使用
2011/10/19 Javascript
javascript通过navigator.userAgent识别各种浏览器
2013/10/25 Javascript
深入理解JSON数据源格式
2014/01/10 Javascript
JavaScript按值删除数组元素的方法
2015/04/24 Javascript
超赞的动手创建JavaScript框架的详细教程
2015/06/30 Javascript
jQuery实现的tab标签切换效果示例
2016/09/05 Javascript
Bootstrap CSS布局之代码
2016/12/17 Javascript
在Python中调用ggplot的三种方法
2015/04/08 Python
Python中正则表达式的详细教程
2015/04/30 Python
Python基于pygame模块播放MP3的方法示例
2017/09/30 Python
python 遍历目录(包括子目录)下所有文件的实例
2018/07/11 Python
python实现简单图片物体标注工具
2019/03/18 Python
Python for循环与range函数的使用详解
2019/03/23 Python
python读写配置文件操作示例
2019/07/03 Python
详解如何用TensorFlow训练和识别/分类自定义图片
2019/08/05 Python
pytorch 获取层权重,对特定层注入hook, 提取中间层输出的方法
2019/08/17 Python
Python编写一个验证码图片数据标注GUI程序附源码
2019/12/09 Python
python实现按关键字筛选日志文件
2019/12/24 Python
python with语句的原理与用法详解
2020/03/30 Python
python 实现读取csv数据,分类求和 再写进 csv
2020/05/18 Python
django model 条件过滤 queryset.filter(**condtions)用法详解
2020/05/20 Python
中兴通讯全球官方网站:ZTE
2020/12/26 全球购物
函授自我鉴定
2013/11/06 职场文书
应届护士求职信范文
2014/01/26 职场文书
国际贸易专业个人职业生涯规划
2014/02/15 职场文书
关于感恩的演讲稿200字
2014/08/26 职场文书
离婚协议书应该怎么写
2014/10/12 职场文书
买房协议书范本
2014/10/23 职场文书
离婚财产分割协议书
2015/08/11 职场文书
《社戏》教学反思
2016/02/22 职场文书
Python first-order-model实现让照片动起来
2022/06/25 Python
Python中np.random.randint()参数详解及用法实例
2022/09/23 Python