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

相關文章
相關標籤/搜索