sqlite3 on python for newbies

时间:2014-07-02 17:46:35   收藏:0   阅读:306

python 集成了 sqlite3 ,其接口很简单:

import sqlite3

db_connection = sqlite3.connect(db_filename)

db_cursor = db_connection.cursor()

db_cursor.execute(‘select * from tt‘)

result_one = db_cursor.fetchone()

result_all = db_cursor.fetchall()

在sqlite 中 有一张 sqlite_master 的表,里边存储的是所有表的建表信息,所以可以通过以下语句查询所有表:

select name from sqlite_master where TYPE = "table"

sqlite 中的 db_cursor.description 是对各列的描述信息:

columnnames = map(lambda x:x[0], db_cursor.description)

sqlite 允许设置数据库读取记录的方法,如下方法可以将结果改为 dict :

db_connection.row_factory = lambda curf, rowf:dict(zip(map(lambda x:x[0], curf.description), rowf))

sqlite3 on python for newbies,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!