Python简单操作sqlite3的方法示例

yipeiwu_com5年前Python基础

本文实例讲述了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编码操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

Django之编辑时根据条件跳转回原页面的方法

在要跳转的编辑页面: #首先获取当期的url: curr_url = self.request.GET.urlencode() #创建一个QueryDict对象: params =...

Python使用itchat模块实现简单的微信控制电脑功能示例

本文实例讲述了Python使用itchat模块实现简单的微信控制电脑功能。分享给大家供大家参考,具体如下: #!/usr/bin/python #coding=UTF-8 impor...

如何使用Python的Requests包实现模拟登陆

如何使用Python的Requests包实现模拟登陆

前段时间喜欢用python去抓一些页面玩,但都基本上都是用get请求一些页面,再通过正则去过滤。 今天试了一下,模拟登陆个人网站。发现也比较简单。读懂本文需要对http协议和http会话...

Python完成毫秒级抢淘宝大单功能

Python完成毫秒级抢淘宝大单功能

引言 年中购物618大狂欢开始了,各大电商又开始了大力度的折扣促销,我们的小胖又给大家谋了一波福利,淘宝APP直接搜索:小胖发福利,每天领取三次粉丝专属现金大红包。 有了现金大红包,如...

Python EOL while scanning string literal问题解决方法

项目中有个定时任务,每天取到一些表数据传到一个外部接口,但是最近总是有异常,今天查了下原因。 首先本地和测试环境测试这个程序都没问题,只有线上环境会在日志中抛出异常,猜测异常主要产生的原...