脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python读取sqlite数据库文件的方法分析

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

2020-11-30 00:33Chunying27 Python

这篇文章主要介绍了Python读取sqlite数据库文件的方法,结合实例形式分析了Python引入sqlite3模块操作sqlite数据库的读取、SQL命令执行等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python读取sqlite数据库文件的方法。分享给大家供大家参考,具体如下:

?
1
import sqlite3

这是Python内置的,不需要pip install 包

数据库里面有很多张表

要操作数据库首先要连接conect数据库

?
1
mydb=sqlite3.connect("alfw.sqlite")

然后创建游标cursor来执行executeSQL语句

?
1
cursor=mydb.cursor()

比如我想看这个数据库的几张表的名字是什么

?
1
2
3
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是表结构理解

?
1
2
3
4
5
6
7
CREATE TABLE sqlite_master (
 type TEXT,
 name TEXT,
 tbl_name TEXT,
 rootpage INTEGER,
 sql TEXT
);

如果要查某一张表Faces的表头结构

?
1
2
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程序设计有所帮助。

延伸 · 阅读

精彩推荐