欢迎投稿

今日深度:

python读取sqlite文件,python读取sqlite

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是表结构理解
<pre name="code" class="html">CREATE TABLE sqlite_master (
  type TEXT,
  name TEXT,
  tbl_name TEXT,
  rootpage INTEGER,
  sql TEXT
);
# referance: <a target=_blank href="http://blog.chinaunix.net/uid-25979788-id-3000955">http://blog.chinaunix.net/uid-25979788-id-3000955</a>


如果要查某一张表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)]






www.htsjk.Com true http://www.htsjk.com/SQLite/26120.html NewsArticle python读取sqlite文件,python读取sqlite import sqlite3 这是python内置的,不需要pip install 包 数据库里面有很多张表 要操作数据库首先要连接conect数据库 mydb=sqlite3.connect("alfw.sqlite") 然后创建游标...
相关文章
    暂无相关文章
评论暂时关闭