网络编程
位置:首页>> 网络编程>> 数据库>> Python读取sqlite数据库文件的方法分析

Python读取sqlite数据库文件的方法分析

作者:Chunying27  发布时间:2024-01-21 11:57:15 

标签: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)]

希望本文所述对大家Python程序设计有所帮助。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com