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程序与系统shell交互的方式
Apr 09 Python
在Django框架中伪造捕捉到的URLconf值的方法
Jul 18 Python
Python自定义简单图轴简单实例
Jan 08 Python
Python高级用法总结
May 26 Python
NumPy 数组使用大全
Apr 25 Python
详解Python图像处理库Pillow常用使用方法
Sep 02 Python
Python使用requests xpath 并开启多线程爬取西刺代理ip实例
Mar 06 Python
Python xlrd excel文件操作代码实例
Mar 10 Python
Python计算信息熵实例
Jun 18 Python
Python中全局变量和局部变量的理解与区别
Feb 07 Python
高考要来啦!用Python爬取历年高考数据并分析
Jun 03 Python
Python学习之os包使用教程详解
Mar 21 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的HTTP请求的库Requests实现美女图片墙
2015/02/22 PHP
PHP匿名函数和use子句用法实例
2016/03/16 PHP
PHP简单实现模拟登陆功能示例
2017/09/15 PHP
PHP join()函数用法与实例讲解
2019/03/11 PHP
Laravel手动返回错误码示例
2019/10/22 PHP
jQuery 位置插件
2008/12/25 Javascript
CCPry JS类库 代码
2009/10/30 Javascript
制作高质量的JQuery Plugin 插件的方法
2010/04/20 Javascript
基于JQuery的类似新浪微博展示信息效果的代码
2012/07/23 Javascript
js获取html文件的思路及示例
2013/09/17 Javascript
Js 去掉字符串中的空格(实现代码)
2013/11/19 Javascript
AngularJS向后端ASP.NET API控制器上传文件
2016/02/03 Javascript
javascript prototype原型详解(比较基础)
2016/12/26 Javascript
js前端实现图片懒加载(lazyload)的两种方式
2017/04/24 Javascript
webpack vue 项目打包生成的文件,资源文件报404问题的修复方法(总结篇)
2018/01/09 Javascript
Node.js使用cookie保持登录的方法
2018/05/11 Javascript
Angular使用cli生成自定义文件、组件的方法
2018/09/04 Javascript
微信小程序如何实现点击图片放大功能
2020/01/21 Javascript
公众号SVG动画交互实战代码
2020/05/31 Javascript
详解datagrid使用方法(重要)
2020/11/06 Javascript
在python中的socket模块使用代理实例
2014/05/29 Python
各种Python库安装包下载地址与安装过程详细介绍(Windows版)
2016/11/02 Python
详解python eval函数的妙用
2017/11/16 Python
numpy中实现二维数组按照某列、某行排序的方法
2018/04/04 Python
python实现汉诺塔算法
2021/03/01 Python
python多个模块py文件的数据共享实例
2019/01/11 Python
Python遍历文件夹 处理json文件的方法
2019/01/22 Python
Python基于opencv调用摄像头获取个人图片的实现方法
2019/02/21 Python
python命名空间(namespace)简单介绍
2019/08/10 Python
Python getsizeof()和getsize()区分详解
2020/11/20 Python
台湾专柜女包:KINAZ
2019/12/26 全球购物
Java的基础面试题附答案
2016/01/10 面试题
公司成本主管岗位责任制
2014/02/21 职场文书
Python 高级库15 个让新手爱不释手(推荐)
2021/05/15 Python
JavaScript数组 几个常用方法总结
2021/11/11 Javascript
基于Python实现将列表数据生成折线图
2022/03/23 Python