数据库 (一) sqlite3

#coding=utf-8
import sqlite3
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

con = sqlite3.connect('/home/flyvideo/sqlite3database.db')
con.text_factory = str # 插入的字符串中含有中文字符,得加这一行
con.execute('create table region (id primary key,name)')  # 只能执行一次  创建表region 包含两个字段,id  name  ,其中id为主键
regions = [('021','上海'),('022','天津'),('023','重庆'),('024','沈阳')]
con.execute("insert into region (id,name) values ('020','广东')")
con.executemany("insert into region (id,name) values (?,?)",regions)
con.execute("update region set name=? where id=?",('广州','020'))
d = con.execute("delete from region where id=?",('024',))
print '删除了',d.rowcount,'行记录'
cur = con.execute('select id,name from region')
for row in cur:
    print row[0],row[1]
con.commit() #事务提交

con.close()

猜你喜欢

转载自blog.csdn.net/nanxiaoting/article/details/82708851