Analysis of how Python reads sqlite database files,
This article describes how to read sqlite database files using Python. We will share this with you for your reference. The details are as follows:
import sqlite3
This is built in Python and does not require the pip install package.
There are many tables in the database
To operate the database, First connect to the conect Database
mydb=sqlite3.connect("alfw.sqlite")
Create a cursor to execute the executeSQL statement.
cursor=mydb.cursor()
For example, I want to see the names of several tables in this database.
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")Tables=cursor.fetchall()print(Tables)
Copy codeThe Code is as follows: >>> [('faces',), ('sqlite _ sequence ',), ('facepose',), ('faceimages ',), ('databases',), ('facemetadata',), ('sqlite _ stat1',), ('facerect ',), ('annotationtype ',), ('faceellipse',), ('nearduplicates ',), ('featurecoords',), ('featurecoordtypes ',)]
This can be understood through sqlite_master as the table structure
CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT);
If you want to query the table Faces header structure
cursor.execute("PRAGMA table_info(Faces)")print cursor.fetchall()
Copy codeThe Code is as follows: >>> [(0, 'Face _ id', 'integer ', 0, None, 1), (1, 'file _ id', 'text ', 1, None, 0), (2, 'db _ id', 'text', 1, None, 0)]