Python可视化数据分析09、MySQL读写
2023-09-14 09:14:16 时间
Python可视化数据分析09、MySQL读写
📋前言📋
💝博客:【红目香薰的博客_CSDN博客-计算机理论,2022年蓝桥杯,MySQL领域博主】💝
✍本文由在下【红目香薰】原创,首发于CSDN✍
🤗2022年最大愿望:【服务百万技术人次】🤗
💝Python初始环境地址:【Python可视化数据分析01、python环境搭建】💝
环境需求
环境:win10
开发工具:PyCharm Community Edition 2021.2
数据库:MySQL5.6
目录
前置环境
pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
pip3 config list
pip3 install --upgrade pip
pip3 install pymysql
数据库
数据表
CREATE TABLE `users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`userName` varchar(255) NOT NULL,
`age` int(11) NOT NULL,
`introduce` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
python链接MySQL
import pymysql # 链接MySQL
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='12345678', db='mytest', charset='utf8')
print(db)
python操作MySQL增删改查
import pymysql # 链接MySQL
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='12345678', db='mytest', charset='utf8')
cursor = db.cursor() # 编写SQL
sqlInsert1 = "insert into users values(0,'{0}',{1},'{2}')".format("雷静", 22, "柔似一泓清水的双眼")
sqlInsert2 = "insert into users values(0,'{0}',{1},'{2}')".format("小凤", 21, "小女孩")
sqlInsert3 = "insert into users values(0,'{0}',{1},'{2}')".format("春梦", 20, "潇洒霸气")
sqlInsert4 = "insert into users values(0,'{0}',{1},'{2}')".format("删除测试", 20, "待删除数据")
cursor.execute(sqlInsert1)
cursor.execute(sqlInsert2)
cursor.execute(sqlInsert3)
cursor.execute(sqlInsert4)
sqlUpdate = "update users set introduce='{0}' where userName='{1}'".format("潇洒的姑娘", "春梦")
cursor.execute(sqlUpdate)
sqlDelete = "delete from users where userName='{0}'".format("删除测试")
cursor.execute(sqlDelete)
# 提交
db.commit()
sql = "select * from users" # 执行SQL
cursor.execute(sql) # 回去返回集合
data = cursor.fetchall() # 遍历集合for item in data:
print(data) # 关闭数据库连接
db.close()
通过以上操作,基本的整个数据库都搞完了,希望能对大家的学习有所价值。
相关文章
- python django 数据库_Apache+Mysql+PHP/Python简单项目
- Mysql 启动与停止的操作方法(mysql启动停止)
- MySQL分析:利用表简化数据分析(mysql分析表)
- PHP操作MySQL数据库实现排名(mysql排名php)
- MySQL日期时间差计算方法详解(mysql两时间相减)
- MySQL玩转关系表:快速创建(mysql创建关系表)
- MySQL 中实现模糊查询的优化策略(mysql模糊查询优化)
- MySQL远程连接指南:解决跨网络问题(mysql 如何远程连接)
- MySQL参数详解,让你更加了解Mysql参数定义及优化。(mysql参数定义)
- Python实现MySQL数据库的读取(python读取mysql)
- MySQL 32位版本助力企业数据分析实现前所未有进展(32位mysql版本)
- MySQL删除表操作的步骤及注意事项,保障数据安全(mysql中删表)
- MySQL分类统计轻松了解数据分析与统计(mysql中分类统计)
- 如何找到可靠的MySQL下载渠道(mysql下载那个一个)