sqlite3 on python for newbies

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

import sqlite3sql

db_connection = sqlite3.connect(db_filename)数据库

db_cursor = db_connection.cursor()fetch

db_cursor.execute('select * from tt')sqlite

result_one = db_cursor.fetchone()接口

result_all = db_cursor.fetchall()ip

在sqlite 中 有一张 sqlite_master 的表,里边存储的是全部表的建表信息,因此能够经过如下语句查询全部表:it

select name from sqlite_master where TYPE = "table"io

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

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))

相关文章
相关标签/搜索