Python讀取sqlite資料庫檔案的方法分析,
本文執行個體講述了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)
複製代碼 代碼如下:>>>[('Faces',), ('sqlite_sequence',), ('FacePose',), ('FaceImages',), ('Databases',), ('FaceMetaData',), ('sqlite_stat1',), ('FaceRect',), ('AnnotationType',), ('FaceEllipse',), ('NearDuplicates',), ('FeatureCoords',), ('FeatureCoordTypes',)]
這個可以通過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()
複製代碼 代碼如下:>>>[(0, 'face_id', 'INTEGER', 0, None, 1), (1, 'file_id', 'TEXT', 1, None, 0), (2, 'db_id', 'TEXT', 1, None, 0)]