Python 中使用SQlite 数据库导论
Python 3.1 提供了使用 SQlite 数据库 的基础模块。
一、导入SQLite 模块
from sqlite3 import dbapi2
二、打开与关闭数据库
conn=dbapi2.connect("c:\\mytest.db")
..... 中间处理过程
conn.close()
解说:
(1) dbapi2.connect("c:\\mytest.db")
打开一个 sqlite 数据库
"c:\\mytest.db" 是要打开的数据库文件名。
如果 c:\mytest.db 文件已存在,则打开,否则以此文件名创建一个库空的sqlite 数据库。
注1 : .db 扩展名不是必须的,您可使用您喜欢的任何扩展名,但是建议不要使用 .exe .dll .c .py .bat .doc .dmp .jpg .gif 等已约定俗成的文件扩展名。
注2: 如果 c:\mytest.db 文件已存在,并且不是 sqlite 数据库,则打开失败。
注3: 如果文件名用 :memory , 则是内存数据库,这在即时交易
系统中非常有用(若干年前,内存数据库要花很多银子才能买到!)。
( 2 ) 当您对数据库操作完毕后,请使用 conn.close() 关闭数据库。
三、在数据库中创建数据表
from sqlite3 import dbapi2
conn=dbapi2.connect("c:\\mytest.db")
sql = "CREATE TABLE IF NOT EXISTS mytb ( a char , b int , c real )"
conn.execute( sql )
conn.close()
解说:
conn.execute ( sql )
( 1 ) CREATE TABLE 表示创建一个数据表 , 该表的名字是 mytb 。
( 2 ) IF NOT EXISTS 表示: 如果数据库中不存 mytb 表的话,就创建 mytb , 如果该表已存在,则什么也不做。
( 3 ) a char , b int , c real 表示该表有三个字段, a 是字符串类型, b 是整数类型, c 是实数类型。
四、 列出SQLite数据库中的所有数据表
使用上面一步创建了数据表后,我们想看一看,数据库中已创建的表的情况:
下面的用法,请参见“六” 节说明。
from sqlite3 import dbapi2
conn=dbapi2.connect("c:\\mytest.db")
sql = "CREATE TABLE IF NOT EXISTS mytb ( a char , b int , c real )"
conn.execute( sql )
cs = conn.cursor( )
cs.execute("SELECT name, sql FROM sqlite_master WHERE type='table'")
recs = cs.fetchall( )
print ( recs )
cs.close()
conn.close()
----- 运行结果: ---------------------
[('mytb', 'CREATE TABLE mytb ( a char , b int , c real )')]
这表明:
我们已成功创建了 表 "mytb" , 使用的语句是 : CREATE TABLE mytb ( a char , b int , c real )
五、 向数据表中加入记录:
#coding=gbk
from sqlite3 import dbapi2
# 打开数据库 c:\mytest.db
conn=dbapi2.connect("c:\\mytest.db")
# 创建数据表 mytb
sql = "CREATE TABLE IF NOT EXISTS mytb ( a char , b int , c real )"
conn.execute( sql )
#向数据表中 加入记录
cs = conn.cursor( )
cs.execute( "INSERT INTO mytb ( a,b,c ) values( '张三',25, 1200.51)" )
cs.execute( "INSERT INTO mytb ( a,b,c ) values( '李四',23, 1300.06)" )
# 将加入的记录保存到磁盘中
conn.commit() # 如果不执行这一句,加入的记录在退出本程序后将丢失。
# 打开数据表
cs.execute( "SELECT * FROM mytb ")
# 取出所有记录
recs = cs.fetchall( )
# recs 是一个数组,每个元素代表一条记录
print ( "共", len(recs),"条记录" )
print ( recs )
cs.close()
conn.close()
--- 结果---:
>>>
共 2 条记录
[('张三', 25, 1200.51), ('李四', 23, 1300.06)]
>>>
六 、 删除记录
#coding=gbk
from sqlite3 import dbapi2
# 打开数据库 c:\mytest.db
conn=dbapi2.connect("c:\\mytest.db")
# 创建数据表 mytb
sql = "CREATE TABLE IF NOT EXISTS mytb ( a char , b int , c real )"
conn.execute( sql )
cs = conn.cursor( )
# 删除 mytb 中字段a 的值为张三的记录
cs.execute("DELETE FROM mytb WHERE A='张三' ")
# 删除ytb 中 的所有记录
cs.execute("DELETE FROM mytb ")
cs.execute( "INSERT INTO mytb ( a,b,c ) values( '张三',25, 1200.51)" )
cs.execute( "INSERT INTO mytb ( a,b,c ) values( '李四',23, 1300.06)" )
cs.execute( "INSERT INTO mytb ( a,b,c ) values( '三五',24, 1800.06)" )
cs.execute( "INSERT INTO mytb ( a,b,c ) values( '刘丽丽',21, 900.01)" )
# 将加入的记录保存到磁盘中
conn.commit() # commit 提交的意思
cs.close()
conn.close()