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程序设计有所帮助。

相关文章

python 如何去除字符串头尾的多余符号

在读文件时常常得到一些\n和引号之类的符号,可以使用字符串的成员函数strip()来去除。 1.去除首尾不需要的字符 a= '"This is test string"' #...

django站点管理详解

管理界面是基础设施中非常重要的一部分。这是以网页和有限的可信任管理者为基础的界面,它可以让你添加,编辑和删除网站内容。Django有自己的自动管理界面。这个特性是这样起作用的:它读取你模...

解决python3 urllib中urlopen报错的问题

解决python3 urllib中urlopen报错的问题

前言 最近更新了Python版本,准备写个爬虫,意外的发现urllib库中属性不存在urlopen,于是各种google,然后总结一下给出解决方案 问题的出现 Attribute...

对Python字符串中的换行符和制表符介绍

有关换行的问题 首先提一个问题,如下。 python程序代码如下: print("I'm Bob. What's your name?") 上一行代码的输出如下: I...

详解Python开发中如何使用Hook技巧

详解Python开发中如何使用Hook技巧

什么是Hook,就是在一个已有的方法上加入一些钩子,使得在该方法执行前或执行后另在做一些额外的处理,那么Hook技巧有什么作用以及我们为什么需要使用它呢,事实上如果一个项目在设计架构时考...