python操作MySQL数据库

python pymysql库的基本操作

创建到MySQL的数据库链接

from pymysql import  Connection
# 构建到MySQL数据库的链接
conn=Connection(
    host="localhost",    #主机名(IP)
    port=3306,          # 端口
    user="root",        # 账号
    password="123456"   # 密码
)
print(conn.get_server_info())

#关闭链接
conn.close()

结果现实数据库版本
在这里插入图片描述

执行非查询性质的sql语句

from pymysql import  Connection
# 构建到MySQL数据库的链接
conn=Connection(
    host="localhost",    #主机名(IP)
    port=3306,          # 端口
    user="root",        # 账号
    password="123456"   # 密码
)
# 获取游标对象
cusor = conn.cursor()
conn.select_db("Student")   #先选择数据库
# 使用游标对象,执行sql语句
cusor.execute("create table test_pymyql(id int,info varchar(255))")
#关闭链接
conn.close()

数据库中成功添加了一个表:

在这里插入图片描述

执行查询性质的sql语句

from pymysql import  Connection
# 构建到MySQL数据库的链接
conn=Connection(
    host="localhost",    #主机名(IP)
    port=3306,          # 端口
    user="root",        # 账号
    password="123456"   # 密码
)
# 获取游标对象
cusor = conn.cursor()
conn.select_db("Student")   #先选择数据库
# 使用游标对象,执行sql语句
cusor.execute("select * from student")
# 获取查询结果
result :tuple = cusor.fetchall()
for r in result:
    print(r)
#关闭链接
conn.close()

在这里插入图片描述

执行数据插入或其他产生数据更改的语句

在执行数据插入等需要更改数据的语句,仅仅只是执行没办法将数据更新到数据库中,
pymysql 在执行数据插入或其他产生数据更改的sql语句时,默认是需要提交更改的,即需要通过代码“确认”这种更改行为。
通过:
链接对象.commit()
即可确认此行为。

commit() 提交

from pymysql import  Connection
# 构建到MySQL数据库的链接
conn=Connection(
    host="localhost",    #主机名(IP)
    port=3306,          # 端口
    user="root",        # 账号
    password="123456"   # 密码
)
# 获取游标对象
cusor = conn.cursor()
conn.select_db("Student")   #先选择数据库
# 使用游标对象,执行sql语句
cusor.execute("insert student values('2022b32005','梦婕',22)")
# 更改确认
conn.commit()
#关闭链接
conn.close()

在这里插入图片描述

自动commit提交

在这里插入图片描述

python操作MySQL数据库综合案例

案例需求:

在这里插入图片描述

数据内容

在这里插入图片描述

ddl定义

在这里插入图片描述

create database py_sql
use py_sql
create  table orders(
	order_date Date,
	order_id varchar(225),
	money int,
	province varchar(10)
);

实现步骤

在这里插入图片描述

实现代码

data_define.py和file_define.py代码和使用面向对象思想进行数据分析的案例中的相同。
main.py

from pymysql import  Connection
from data_define import Record
from file_define import TextFileReader,JsonFileReader,FileReader
from pyecharts.charts import Bar
from pyecharts.options import *
from pyecharts.globals import ThemeType

textfilereader=TextFileReader(r"C:\cwy\slsd\毕设\python学习\资料\第13章资料\2011年1月销售数据.txt")
jsonfilereader = JsonFileReader(r"C:\cwy\slsd\毕设\python学习\资料\第13章资料\2011年2月销售数据JSON.txt")
# 调用类方法获得文件内容,存储为list
jan_data: list[Record] = textfilereader.read_data()
feb_data: list[Record] = jsonfilereader.read_data()
#将两个月份的list合并成一个list来存储
all_data=jan_data + feb_data

# 构建到MySQL数据库的链接
conn=Connection(
    host="localhost",    #主机名(IP)
    port=3306,          # 端口
    user="root",        # 账号
    password="123456",   # 密码
    autocommit=True      #自动更改确认
)
# 获取游标对象
cusor = conn.cursor()
conn.select_db("py_sql")   #先选择数据库
#组织sql语句
for record in all_data:
    sql=f"insert into orders values('{record.date}','{record.order_id}',{record.meoney},'{record.province}')"
    # 使用游标对象,执行sql语句
    print(sql)
    cusor.execute(sql)
    # 更改确认
    #conn.commit()
#关闭链接
conn.close()

成果:写入数据库成功
在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老年断牙人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值