0
点赞
收藏
分享

微信扫一扫

Python可视化数据分析09、MySQL读写


Python可视化数据分析09、MySQL读写_mysql

Python可视化数据分析09、MySQL读写


🤗2022年最大愿望:【服务百万技术人次】🤗

💝Python初始环境地址:【​​Python可视化数据分析01、python环境搭建​​】💝 

环境需求

环境:win10

开发工具:PyCharm Community Edition 2021.2

数据库:MySQL5.6

目录

​​Python可视化数据分析09、MySQL读写​​

​​📋前言📋​​

​​环境需求​​

​​前置环境​​

​​数据库​​

​​数据表​​

​​python链接MySQL​​

​​python操作MySQL增删改查​​

前置环境

pip3 config set global.index-url https://repo.huaweicloud.com/repository/pypi/simple
pip3 config list
pip3 install --upgrade pip
pip3 install pymysql

Python可视化数据分析09、MySQL读写_mysql_02

数据库

Python可视化数据分析09、MySQL读写_sql_03

数据表

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可视化数据分析09、MySQL读写_mysql_04

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可视化数据分析09、MySQL读写_python_05

Python可视化数据分析09、MySQL读写_sql_06

通过以上操作,基本的整个数据库都搞完了,希望能对大家的学习有所价值。

 

举报

相关推荐

0 条评论