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

第一part:pymysql模块的安装:

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

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

2114399-20201007153744696-1379701839.png

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

2114399-20201007153601458-1715378874.png

第二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()

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

2114399-20201007160224467-1964314871.png

第三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()

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

2114399-20201007161053547-565241536.png

第四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()

执行之后,结果如下:

2114399-20201007161646369-678178292.png

第五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()

执行之后,结果如下:

2114399-20201007162036404-1059078098.png

第六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()

执行之后,结果如下:

2114399-20201007163154434-41453277.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值