Database programming interface: python database API
python database API overview
python database API specification For access to relational databases, the Python community has developed a standard called Python Database API. Through this interface, Python's operation code across different databases can be more portable, allowing Python to connect (and operate) More databases.
There are two main objects in the python database API:
- A Connection object used to manage database connections
- Cursor object used to execute queries
connection object
connect() function
This function creates and returns a Connection object, and the connect() function accesses the database through the Connection object
This function has several parameters, which parameter to use depends on the type of database used. If you want to access Oracle and MySQL databases, you must download these two python modules at the same time. These modules all require the connect() function when obtaining a connection object.
Connect() function properties:
user username
password password
host host name
database database name MySQLdb database uses db
dsn data source name, specify the database type to be connected, such as: mysql, oracle
Connection connection object
Mainly used to provide database cursor objects and methods for committing and rolling back transactions and closing database connections
Common methods of Connection objects:
close() closes the database connection
commit() commits the transaction
rollback() rolls back the transaction
cursor() obtains the cursor object and operates the database such as: performing DML operations, calling stored procedures, etc.
cursor object
cursor(): Generate a cursor object through the cursor() method of the Connection object
The common methods of cursor objects are as follows:
Execution order:
Create a database connection object → create a cursor object → execute SQL statements to process data results → close the cursor → close the connection object
Use the sqlite3 module to operate SQLite databases
SQLite: Embedded database, its database is a file, so it is easy to use and easy to operate
sqlite3: The built-in SQLite module in python is
Create database file
Example: A database file will be created in the current directory
import sqlite3
# 连接到SQLite数据库
# 数据库文件是mrsoft.db
# 如果文件不存在,会自动在当前目录创建:
conn = sqlite3.connect('mrsoft.db') #mrsoft.db表示定义的数据库名称
# 创建一个Cursor:
cursor = conn.cursor()
# 执行一条SQL语句,创建user表:
cursor.execute('create table user (id int(10) primary key, name varchar(20))')
# 关闭游标
cursor.close()
# 提交事务:
conn.commit()
# 关闭Connection:
conn.close()
Working with SQLite databases
Note: Because the quotation marks appear in pairs, when writing a SQL statement, it must be single quotation marks outside and double quotation marks inside or vice versa.
Install the graphical tool Navicat for salite and perform a viewing test after installation
New
- Add new data
Example:
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
cursor.execute('insert into user (id,name) values(1,"小明")')
cursor.execute('insert into user (id, name) values ("2", "Andy")')
cursor.close()
conn.commit()
conn.close()
- In order to avoid sql injection and leak information, sql statements can be written with placeholders
Example:
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
sql = 'insert into user (id,name) values(?,?)' #?占位符进行预处理
cursor.execute(sql,(2,'lisi')) #用真实的值替换占位符,一一对应的关系
cursor.close()
conn.commit()
conn.close()
- Insert data in batches through the cursor.executemany method
Example:
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
sql = 'insert into user (id,name) values(?,?)' #?占位符进行预处理
data = [(3,'wang3'),(4,'wang4'),(5,'wang5')] #定义列表
cursor.executemany(sql,data) #批量插入
cursor.close()
conn.commit()
conn.close()
Inquire
Note: The query does not require a transaction submission
- fetchone(): Get the next record in the cluster result set and return a tuple
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
cursor.execute('select * from user')
print(cursor.fetchone()) #获取第一条记录
print(cursor.fetchone()) #获取第二条记录
print(cursor.fetchone()) #获取第三条记录
cursor.close()
conn.close()
- fetchmany(size): Get the specified number of records and return a list
#size: Specifies how many records to get in the result set, the default is one
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
cursor.execute('select * from user') #
print(cursor.fetchmany(2)) #指定获取结果集中的两条记录
cursor.close()
conn.close()
- fetchall: Get all the records in the structure set and return a list
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
cursor.execute('select * from user where id > 3')
print(cursor.fetchall())
cursor.close()
conn.close()
Revise
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
sql = 'update user set name = ? where id = ?'
cursor.execute(sql,('wang1',1))
cursor.close()
conn.commit()
conn.close()
delete
import sqlite3
conn = sqlite3.connect('mrsift.db')
cursor = conn.cursor()
sql = 'delete from user where id = ?'
cursor.execute(sql,[1])
cursor.close()
conn.commit()
conn.close()
Using MySQL database
Download and install MySQL
Install PyMySQL
Third-party module PyMySQL : Because there is no built-in module for operating mysql in python, we need to install a third-party mysql operating module. There are many supported modules, and PyMySQL is one of them.
Command line execution installation : pip3 install PyMySQL
Connect to the database
Example
import pymysql
# 打开数据库连接,参数1:主机名或IP;参数2:用户名;参数3:密码;参数4:数据库名称
db = pymysql.connect(host="localhost", user="root", password="123456", database="mrsoft")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print ("Database version : %s " % data)
cursor.close()
# 关闭数据库连接
db.close()
Create data table
Example
import pymysql
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="123456", database="mrsoft")
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS books")
# 使用预处理语句创建表
sql = """
CREATE TABLE books (
id int(8) NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
category varchar(50) NOT NULL,
price decimal(10,2) DEFAULT NULL,
publish_time date DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
"""
# 执行SQL语句
cursor.execute(sql)
cursor.close()
# 关闭数据库连接
db.close()
operating data sheet
You can refer to sqlite3 for deletion, modification and query, and the operation statements are basically the same
Example: Add data and insert multiple items at one time
Note: The placeholder for sqlite is '?', while the placeholder for mysql is '%s'
import pymysql
# 打开数据库连接
db = pymysql.connect(host="localhost", user="root", password="Picc@1234", database="mrsoft") #如果有汉字需要指定字符编码否则报错
# 使用cursor()方法获取操作游标
cursor = db.cursor()
# 数据列表
data = [("零基础学Python",'Python','79.80','2018-5-20'),
("Python从入门到精通",'Python','79.80','2018-10-1'),
("Python数据分析从入门到实践",'Python','98.00','2020-6-1'),
("Java从入门到精通(第5版)",'Java','69.80','2019-2-1'),
("零基础学Java",'Java','69.80','2017-5-18'),
]
try:
# 执行sql语句,插入多条数据
cursor.executemany("insert into books(name, category, price, publish_time) values (%s,%s,%s,%s)", data)
# 提交数据
db.commit()
except:
# 发生错误时回滚
db.rollback()
cursor.close()
# 关闭数据库连接
db.close()