Python reads SQLite file data

Source: Internet
Author: User
Tags read sqlite file sqlite sqlite database

  Recently in the project, accidentally heard that there is a database of sqlite, compared to their previous use of SQL Service is very light, in the data integrity, concurrency requirements are not high in the scenario can be tried! 1, SQLite Introduction: SQLite is an in-process library, the implementation of a self-contained, server-free, 0-configured, transactional SQL database engine. It is designed to be embedded, and has been used in many embedded products (such as Android), it occupies a very low resource, in the embedded device, may only need hundreds of K of memory is enough.  It can support mainstream operating systems such as Windows/linux/unix, and can be combined with many programming languages, such as Python, C #, PHP, Java, and ODBC interfaces. SQLite engine is not a "program communicates with" The independent process, but connected to the program as a major part of it. So the main communication protocol is the direct API call within the programming language. This has a positive effect on total consumption, delay time, and overall simplicity. The entire database (definitions, tables, indexes, and data itself) is stored in a single file on the host host.  Its simple design is done by locking the entire data file at the start of a transaction. 2, SQLite file management: SQLite file suffix is. db, you can use the SQLite database management tools to view its content, such as Sqlitestudio is a SQLite database visualization tool, is the use of SQLite database development of applications, software prerequisites,  The software does not need to be installed and can be extracted after downloading. is a simple engineering example, ARPA data is stored in the SQLite library and defines a arpainfo table with ID, time, and Arpa three fields, where data in the tab can be seen in the data stored in the file. The ease of use of SQLite's embedded databases accelerates application development and enables small applications to fully support complex SQL, so it is not necessary to use text files for persistent storage.   3, Python read SQLite file SQLite3 can be integrated with Python using the Sqlite3 module. The Sqlite3 module was written by Gerhard Haring. It provides a SQL interface that is compatible with the DB-API 2.0 specification described in PEP 249. You do not need to install the module separately, since Python version 2.5.x or later has the module as its default. In order to use the Sqlite3 module, you must first create a Connection object that represents the database, and then you can selectively create the cursor object, which will help you perform all the SQL statement. A description of the Python Sqlite3 module API can be viewed at the following link.  Http://www.runoob.com/sqlite/sqlite-python.html (Chinese) and https://docs.python.org/2/library/sqlite3.html (English). The latest code can view my github:https://github.com/wylloong/tinyprograms/blob/master/accesssqlitebypython******************** code example:
#-*-Coding:utf8-*-" "Author:wang yanlongdate:2017-08-16all rights reserved. Distributed under the BSD license. References:https://docs.python.org/2/library/sqlite3.html (中文版) http://www.runoob.com/sqlite/ Sqlite-python.html (Chinese)" "ImportSqlite3 as DB#reading data from SQLite filesdefReadfronsqllite (db_path,exectcmd): Conn= Db.connect (Db_path)#The API opens a link to the SQLite database file, and returns a Connection object if the database opens successfullyCursor=conn.cursor ()#The routine creates a cursor that will be used in Python database programming. Conn.row_factory=db. Row#access to column informationCursor.execute (Exectcmd)#the routine executes an SQL statementRows=cursor.fetchall ()#the routine gets all (the remaining) rows in the query result set, returning a list. When no rows are available, an empty list is returned.     returnrows#print (rows[0][2]) # Select a column of data#parsing arpa single frame informationdefReadfromappaframe (arpaframe): Subarpa=arpaframe.split (',')    Print(SUBARPA)if __name__=="__main__": Rows=readfronsqllite ('e://arpa.db',"Select ARPA from Arpainfo") ReadLines=10010Lineindex=10000 whilelineindex<Readlines:row=rows[lineindex]#gets the data for a row, the type is a tupleContent="". Join (ROW)#tuple to StringReadfromappaframe (content)#parsing ARPA dataLineindex+=1

***************************************

Examples of program run results:

Python reads SQLite file data

Related Article

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.