How to use Python to operate various functions of MySQL
Jun 03, 2023 pm 12:37 PMConnecting to MySQL
In Python, we can use the pymysql
library to connect to the MySQL database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
In the above code, we first used the pymysql
library to connect to the MySQL database and obtain the cursor. Then, we executed a simple SELECT
statement and obtained the result set. Finally, we close the cursor and connection.
CRUD
In MySQL, we can use INSERT
, DELETE
, UPDATE
and SELECT
statement to complete the add, delete, modify and check operations. In Python, we can also use the pymysql
library to perform these operations.
Insert data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
In the above code, we use the INSERT
statement to insert a piece of data into the users
table. When executing the execute
method, we can use the placeholder %s
to represent parameters, and then pass in the corresponding parameters during execution. Finally, we commit the transaction and close the cursor and connection.
Delete data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
In the above code, we use the DELETE
statement to delete the id
of 1 in the users
table data. When executing the execute
method, we also use the placeholder %s
to represent the parameters. Finally, we commit the transaction and close the cursor and connection.
Update data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
In the above code, we use the UPDATE
statement to update the username
in the users
table to ## The password for #Tom’s data. When executing the
execute method, we also use the placeholder
%s to represent the parameters. Finally, we commit the transaction and close the cursor and connection.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pymysql
# 连接MySQL
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8')
# 获取游标
cursor = conn.cursor()
# 查询数据
sql = "SELECT * FROM users WHERE username = %s"
params = ('Tom',)
cursor.execute(sql, params)
# 获取结果集
result = cursor.fetchall()
print
(result)
# 关闭游标和连接
cursor.close()
conn.close()
Copy after login
In the above code, we use the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
SELECT statement to query the
username in the
users table for
Tom's data. When executing the
execute method, we also use the placeholder
%s to represent the parameters. Finally, we retrieve the result set and close the cursor and connection.
INSERT,
DELETE,
UPDATE and
SELECT statement to operate data in batches. In Python, we can also use the
pymysql library to operate data in batches.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pymysql
# 连接MySQL
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8')
# 获取游标
cursor = conn.cursor()
# 批量插入数据
sql = "INSERT INTO users(username, password) VALUES (%s, %s)"
params = [('Tom', '123456'), ('Jerry', '654321'), ('Alice', '111111')]
cursor.executemany(sql, params)
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
Copy after login
In the above code, we use the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
executemany method to batch insert multiple pieces of data. When executing the
executemany method, we use a tuple list to represent multiple parameters. Finally, we commit the transaction and close the cursor and connection.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pymysql
# 连接MySQL
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8')
# 获取游标
cursor = conn.cursor()
# 批量删除数据
sql = "
DELETE
FROM users WHERE id = %s"
params = [(1,), (2,), (3,)]
cursor.executemany(sql, params)
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
Copy after login
In the above code, we use the 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
executemany method to delete multiple pieces of data in batches. When executing the
executemany method, we also use a tuple list to represent multiple parameters. Finally, we commit the transaction and close the cursor and connection.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import pymysql
# 连接MySQL
conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8')
# 获取游标
cursor = conn.cursor()
# 批量更新数据
sql = "UPDATE users SET password = %s WHERE username = %s"
params = [('123456', 'Tom'), ('654321', 'Jerry'), ('111111', 'Alice')]
cursor.executemany(sql, params)
# 提交事务
conn.commit()
# 关闭游标和连接
cursor.close()
conn.close()
Copy after login
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The above is the detailed content of How to use Python to operate various functions of MySQL. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to optimize MySQL query performance in PHP?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP?
