Table of contents
database preparation
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;
First, you need to install the pymysql package in the console of this project
pip install pymysql
Query data
Import the pymysql package
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
# sql语句
sql="select * from t_user;"
# 执行sql
content=cs.execute(sql)
# 打印结果
print(content)
# 关闭游标和连接
cs.close()
conn.close()
Execute the code and find that only the number of data is obtained.
So how to get the data, use the cs.fetchone() method
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
# sql语句
sql="select * from t_user;"
# 执行sql
content=cs.execute(sql)
# 获取单个数据
content=cs.fetchone()
# 打印结果
print(content)
In this way, only one row of data is obtained, so how to obtain all the data, you need to use the cs.fetchall() method
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
# sql语句
sql="select * from t_user;"
# 执行sql
content=cs.execute(sql)
# 获取所有数据
content=cs.fetchall()
# 打印结果
print(content)
# 关闭游标和连接
cs.close()
conn.close()
Python returns all data in the form of tuples
increase data
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
sql="insert into t_user value(null,'宋江',23,'河北省');"
cs.execute(sql)
# sql语句
sql="select * from t_user;"
# 执行sql
content=cs.execute(sql)
content=cs.fetchall()
# 打印结果
print(content)
# conn.commit()
# 关闭游标和连接
cs.close()
conn.close()
You can see that a data with an id of 6 is added to the output result
But when we checked in the database, we found that there were still 5 pieces of data, and the data with id 6 was not added.
The reason why this happens is that python does not automatically commit the transaction, so you need to manually commit the transaction and manually call the conn.commit() method
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
sql="insert into t_user value(null,'宋江',23,'河北省');"
cs.execute(sql)
# sql语句
sql="select * from t_user;"
# 执行sql
content=cs.execute(sql)
content=cs.fetchall()
# 打印结果
print(content)
conn.commit()
# 关闭游标和连接
cs.close()
conn.close()
Note: The reason why the new data is 7 here is because the id is self-incrementing, and the transaction has not been submitted before, but the id number has been used, so adding it again will start from 7.
delete
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
# sql语句
sql="delete from t_user where id='7';"
# 执行sql
content=cs.execute(sql)
conn.commit()
# 关闭游标和连接
cs.close()
conn.close()
Start the program and find that the data with id 7 has been deleted. Note: you must manually submit the transaction
Revise
import pymysql
# 创建连接对象
conn=pymysql.connect(host="localhost",port=3306,user="root",password="xxx",database="python_test")
# 获取游标对象
cs=conn.cursor()
# sql语句
sql="update t_user set name='张飞' where id='5';"
# 执行sql
content=cs.execute(sql)
conn.commit()
# 关闭游标和连接
cs.close()
conn.close()