python对sqlite数据库的增删查改操作

阅读更多

 

    db.py内容如下:

#导入Python SQLITE数据库模块(Python2.5之后,已内置了SQLite3)
import sqlite3
#创建/打开数据库 
cx = sqlite3.connect("/Users/lijie/pythonworkspace/webpy/db/sqlite.db")
#con = sqlite3.connect(":memory:")
cu=cx.cursor()
#创建表
#cu.execute("create table catalog (id integer primary key,pid integer,name varchar(10) UNIQUE,nickname text NULL)")

#插入数据
for t in[(0,10,'abc','Yu'),(1,20,'cba','Xu')]:
    cx.execute("insert into catalog values (?,?,?,?)", t)
cx.commit()

#查询
cu.execute("select * from catalog") 
#提取查询到的数据
for item in cu.fetchall()
    for element in item:
    	print element,
    print
    	
#修改
cu.execute("update catalog set name='Boy' where id = 0")
cx.commit()

#删除
cu.execute("delete from catalog where id = 1")  
cx.commit()

 

 

参考:

http://www.cnblogs.com/yuxc/archive/2011/08/18/2143606.html

你可能感兴趣的:(sqlite)