Table of Contents
Connecting to MySQL
CRUD
Insert data
Delete data
Update data
Home Database Mysql Tutorial How to use Python to operate various functions of MySQL

How to use Python to operate various functions of MySQL

Jun 03, 2023 pm 12:37 PM
mysql python

Connecting 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

import pymysql

 

# 连接MySQL

conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8')

 

# 获取游标

cursor = conn.cursor()

 

# 执行SQL语句

cursor.execute('SELECT * FROM users')

 

# 获取结果集

result = cursor.fetchall()

print(result)

 

# 关闭游标和连接

cursor.close()

conn.close()

Copy after login

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

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')

cursor.execute(sql, params)

 

# 提交事务

conn.commit()

 

# 关闭游标和连接

cursor.close()

conn.close()

Copy after login

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

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,)

cursor.execute(sql, params)

 

# 提交事务

conn.commit()

 

# 关闭游标和连接

cursor.close()

conn.close()

Copy after login

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

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 = ('654321', 'Tom')

cursor.execute(sql, params)

 

# 提交事务

conn.commit()

 

# 关闭游标和连接

cursor.close()

conn.close()

Copy after login

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.

Query data

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

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.

Batch operation

In MySQL, we can use

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.

Batch Insert Data

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

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.

Delete 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 = "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

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.

Batch update data

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

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to download deepseek Xiaomi How to download deepseek Xiaomi Feb 19, 2025 pm 05:27 PM

How to download deepseek Xiaomi

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Google AI announces Gemini 1.5 Pro and Gemma 2 for developers Jul 01, 2024 am 07:22 AM

Google AI announces Gemini 1.5 Pro and Gemma 2 for developers

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

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 fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

How do you ask him deepseek How do you ask him deepseek Feb 19, 2025 pm 04:42 PM

How do you ask him deepseek

See all articles