python sqlite3 usage, performance and limitations

sqlite3 usage, performance and limitations

Using sqlite3 in python

The first is the basic use:

# coding=utf8
__author__ = 'Administrator'

# Import the module, which is already built-in in python, so you don't need to install it and use it directly
import sqlite3

# Create a connection to the database, sqlite is stored in a local file, open directly here
conn = sqlite3.connect('path_to_database_name.db', timeout=5)

# Comparing with mysql, you can connect to the database through the network, so you need to add the corresponding protocol, user, password, address, database, encoding, etc. to construct a similar connection string

# operate

# get the cursor
cursor = conn.cursor()
# The operation of the database table is carried out through the cursor, so the cursor object should be obtained before the operation, and it should be closed after use.

# query an object
cursor.execute("select * from tb_student where age > ?", (15, ))
# Here is a query statement executed through the cursor, in which the corresponding parameters can be constructed and set through placeholders, which is very convenient without string splicing.
# But the table name still needs to be written honestly, the placeholder will be abnormal, here is a pit

# retrieve data
result = cursor.fetchall()
result = cursor.fetchmany(20)
result = cursor.fetchone()
# Get all, get a certain number, get one
# The result type is a list or tuple, and the tuple is the field in the corresponding database
# You can pack it yourself, name it with namedtuple, and you can operate like an object's property acquisition! ! !

# Finally, be sure to close the connection after the operation is complete
conn.close()
# The best practice here is to use python's context manager so you don't have to close every time

Next are some of its features:

  • Concise API is very concise, easy to use and easy to use
  • Lightweight Zero configuration, no need to install configuration management
  • Can be written in C language, compact and easy to embed into other devices
  • No network used in some terminals, very suitable
  • Fast Except that the performance of high concurrent writes may be lower than mysql postgresql, the others are not slow
  • There are many more

Here:
        The main purpose of this project is to choose a small, efficient, data storage thing that does not require a network connection, Dingdong, sqlite, just right! ! !
        And sqlite3 also provides some operation interfaces, which is more convenient!

A performance and limitation analysis article

no more~~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325873664&siteId=291194637