Home > Backend Development > Python Tutorial > How to read files from sqlite database in Python?

How to read files from sqlite database in Python?

黄舟
Release: 2017-08-07 15:34:11
Original
3985 people have browsed it

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
Copy after login

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")
Copy after login

Then create a cursor to execute the executeSQL statement


cursor=mydb.cursor()
Copy after login

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 after login

Copy code The code is as follows:

>>>[('Faces',), ('sqlite_sequence',), ('FacePose',), ('FaceImages',), ('Databases',), ('FaceMetaData',), ('sqlite_stat1',), ('FaceRect',), ('AnnotationType',), ('FaceEllipse',), ('NearDuplicates',), ('FeatureCoords',), ('FeatureCoordTypes',)]
Copy after login

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
);
Copy after login

If you want to check the header structure of a certain table Faces


cursor.execute("PRAGMA table_info(Faces)")
print cursor.fetchall()
Copy after login

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)]
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template