📋前言📋
💝博客:【红目香薰的博客_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;
- import pymysql # 链接MySQL
-
- db = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='12345678', db='mytest', charset='utf8')
-
- print(db)
- 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()
通过以上操作,基本的整个数据库都搞完了,希望能对大家的学习有所价值。