Analysis of how Python reads sqlite database files,

Source: Internet
Author: User

Analysis of how Python reads sqlite database files,

This article describes how to read sqlite database files using Python. We will share this with you for your reference. The details are as follows:

import sqlite3

This is built in Python and does not require the pip install package.

There are many tables in the database

To operate the database, First connect to the conect Database

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

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 codeThe Code is as follows: >>> [('faces',), ('sqlite _ sequence ',), ('facepose',), ('faceimages ',), ('databases',), ('facemetadata',), ('sqlite _ stat1',), ('facerect ',), ('annotationtype ',), ('faceellipse',), ('nearduplicates ',), ('featurecoords',), ('featurecoordtypes ',)]

This can be understood through sqlite_master as the table structure

CREATE TABLE sqlite_master ( type TEXT, name TEXT, tbl_name TEXT, rootpage INTEGER, sql TEXT);

If you want to query the table Faces header structure

cursor.execute("PRAGMA table_info(Faces)")print cursor.fetchall()

Copy codeThe 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)]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.