本文實例講述了Python讀取sqlite數據庫文件的方法。分享給大家供大家參考,具體如下:
import sqlite3
這是Python內置的,不需要pip install 包
數據庫里面有很多張表
要操作數據庫首先要連接conect數據庫
mydb=sqlite3.connect("alfw.sqlite")然后創建游標cursor來執行executeSQL語句
cursor=mydb.cursor()
比如我想看這個數據庫的幾張表的名字是什么
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")Tables=cursor.fetchall()print(Tables)
這個可以通過sqlite_master是表結構理解
CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT);
如果要查某一張表Faces的表頭結構
cursor.execute("PRAGMA table_info(Faces)")print cursor.fetchall()
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答