python操作MySQL--PyMySql模块(第三方)

第一part:pymysql模块的安装:


 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个第三方库,Python2中则使用mysqldb,其安装方式有两种:

方式一:在命令行L:pip install pymysql

方式二:在pycharm --settings--project---project Interpreter中搜索PyMySql进行添加

 


 第二part:pymysql对数据库的连接--新增数据



#1:导入pymysql模块

import pymysql
#2:连接mysql数据库,使用connect函数,其中包含数据库服务器地址、端口号,用户名、密码、数据库名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:创建游标,用于获取结果集
get_cursor=get_connect.cursor()
#4:定义一条插入的sql语句
insert_sql="insert into student values(7,'huangsasa','广州白云')"
#5:使用游标进行执行sql
get_cursor.execute(insert_sql)
#6:进行事务提交(只有新增,修改和删除需要)
get_connect.commit()
#7:关闭游标连接
get_cursor.close()
#8:关闭数据库连接
get_connect.close()

执行之后,在表中新增一条数据:



 第三part:pymysql对数据库的连接--修改数据


 #1:导入pymysql模块

import pymysql
#2,连接mysql数据库,使用connect函数,其中包含数据库服务器地址、端口号,用户名、密码、数据库名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:创建游标,用于获取结果集
get_cursor=get_connect.cursor()
#4:定义一条更新的sql语句
update_sql="update student set student_address='深圳市罗湖区' where student_id=7"
#5:使用游标进行执行sql
get_cursor.execute(update_sql)
#6:进行事务提交
get_connect.commit()
#7:关闭游标连接
get_cursor.close()
#8:关闭数据库连接
get_connect.close()

执行之后,在表中对该条数据进行修改,如下:

 


 第四part:查询数据


 #1:导入pymysql模块

import pymysql
#2,连接mysql数据库,使用connect函数,其中包含数据库服务器地址、端口号,用户名、密码、数据库名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:创建游标,用于获取结果集
get_cursor=get_connect.cursor()
#4:定义一条查询的sql语句
select_sql="select * from student where student_id=7"
#5:使用游标进行执行sql
get_cursor.execute(select_sql)
#6:获取执行的结果,fetchall为查询所有的记录
results=get_cursor.fetchall()
#7:打印查询结果,结果的类型为元组,可以通过循环进行遍历打印
print(results)
#8:关闭游标
get_cursor.close()
#9:关闭数据库连接
get_connect.close()

执行之后,结果如下:

 


 第五part:删除数据


 #1:导入pymysql模块

import pymysql
#2,连接mysql数据库,使用connect函数,其中包含数据库服务器地址、端口号,用户名、密码、数据库名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:创建游标,用于获取结果集
get_cursor=get_connect.cursor()
#4:定义一条删除的sql语句
delete_sql="delete from student where student_id=7"
#5:使用游标进行执行sql
get_cursor.execute(delete_sql)
#6:进行事务提交
get_connect.commit()
#7:关闭游标连接
get_cursor.close()
#8:关闭数据库连接
get_connect.close()

 执行之后,结果如下:

 

 


 

第六part:对新增,修改和删除失败时进行回滚数据


 #1:导入pymysql模块

import pymysql
#2,连接mysql数据库,使用connect函数,其中包含数据库服务器地址、端口号,用户名、密码、数据库名
get_connect=pymysql.connect(host="localhost",
port=3306,
user="root",
passwd="123456",
db="school")
#3:创建游标,用于获取结果集
get_cursor=get_connect.cursor()
#4:定义一条sql语句
str_sql="insert into student values (7,'huigun','广州海珠区')"
error_sql="insert into student values ('huigun','失败时进行回滚操作')"
# 5:使用游标进行执行sql
try:
#执行一个正确的sql语句
get_cursor.execute(str_sql)
#执行一个错误的sql语句
get_cursor.execute(error_sql)
#提交事务
get_connect.commit()
except Exception as e:
#出现异常,事务进行回滚操作
get_connect.rollback()
print("抛出异常,事务进行回滚操作")
#6:进行事务提交
get_connect.commit()
#7:关闭游标连接
get_cursor.close()
#8:关闭数据库连接
get_connect.close()

执行之后,结果如下:

 

posted @ 2020-10-07 16:32  小珊子  阅读(404)  评论(0编辑  收藏  举报