This article mainly introduces the method of Python reading sqlite database files, and analyzes the related operation skills of Python introducing the sqlite3 module to operate sqlite database reading, SQL command execution and other related operation techniques in the form of examples. Friends in need can refer to it
The example in this article describes how Python reads sqlite database files. Share it with everyone for your reference, the details are as follows:
import sqlite3
This is built-in in Python and does not require pip install package
There are many tables in the database
To operate the database, you must first connect to the conect database
mydb=sqlite3.connect("alfw.sqlite")
Then 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 code The code is as follows:
>>>[('Faces',), ('sqlite_sequence',), ('FacePose',), ('FaceImages',), ('Databases',), ('FaceMetaData',), ('sqlite_stat1',), ('FaceRect',), ('AnnotationType',), ('FaceEllipse',), ('NearDuplicates',), ('FeatureCoords',), ('FeatureCoordTypes',)]
This can be understood through the table structure of sqlite_master
CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT );
If you want to check the header structure of a certain table Faces
cursor.execute("PRAGMA table_info(Faces)") print cursor.fetchall()
Copy code The 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)]
The above is the detailed content of How to read files from sqlite database in Python?. For more information, please follow other related articles on the PHP Chinese website!