![](https://img-blog.csdnimg.cn/img_convert/8968facf06d6441b90501588d2e76318.jpeg)
第1步:写python代码连mysql,并打印mysql版本号:
import pymysql
# 打开数据库连接
try:
db = pymysql.connect(host='localhost', user='root', passwd='', port=3306)
print('连接成功!')
except:
print('something wrong!')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print("Database version : %s " % data)
# 关闭数据库连接
db.close()
以上代码参考自知乎文章【Python操作Mysql,这一篇就够了】
第2步:在cmd执行py hyb007_mysql.py想跑下代码时,发现报错:
![](https://img-blog.csdnimg.cn/img_convert/6052aa0d75a54ae894646e89d8ca7cce.png)
2.1 如上图报错提示:模块找不到错误,没有pymysql模块。
2.2 于是,在cmd执行pip install pymysql,希望安装pymysql模块,但还是报错说'pip' 不是内部或外部命令,也不是可运行的程序或批处理文件。
![](https://img-blog.csdnimg.cn/img_convert/8822075aa66b400fa47606b61bfce8c9.png)
好吧,这就进入本文的pip安装专题。
第3步:
3.1 在cmd中分别执行python --version和python -m pip --version,发现我在淘宝npmmirror镜像站下载的这个python-3.9.0b5-amd64.exe确实没有安装pip
![](https://img-blog.csdnimg.cn/img_convert/944445c977de43cea7b1557ccb47b497.png)
3.2 于是在cmd中执行py -m ensurepip --default-pip启用标准库中的pip(注意:py和python命令是一样的!)
![](https://img-blog.csdnimg.cn/img_convert/c719532ed57c49cf9a1c87fd7b728a5c.png)
3.3 安装pip成功后,再执行下py -m pip --version,发现pip版本是20.1.1
![](https://img-blog.csdnimg.cn/img_convert/f707cf582c784338826933dac019ab04.png)
3.4 安装pip成功,我们就可以安装pymysql模块了,执行py -m pip install pymysql
![](https://img-blog.csdnimg.cn/img_convert/e0bca560d1964142b338cb8ec76acbb0.png)
第4步:最后我们执行本文开头的python代码,连接mysql并获取其版本号,py hyb007_mysql.py
![](https://img-blog.csdnimg.cn/img_convert/17c6da6734de43ffa0464cff27f01c5b.png)
4.1 以上报错,是因为mysql数据库没有启动,于是我启动[DB Mysql 8.0.30 x64 for Windows超级精简便携版.7z]:
![](https://img-blog.csdnimg.cn/img_convert/7b0cf0713e594e238bf8a241c9bebb30.png)
4.2 再次在cmd执行代码py hyb007_mysql.py
![](https://img-blog.csdnimg.cn/img_convert/d15f349c75f3438489d67677b2cd52d2.png)
可以看到代码执行成功,我win7电脑安装的mysql的版本号是 8.0.30
第5步:接上,我们再把对数据库的增删改查操作coding出来,下面贴出代码(代码可正常运行)
import pymysql
# 打开数据库连接
try:
# db = pymysql.connect(host='localhost', user='root', passwd='', port=3306)
db = pymysql.connect(host='localhost', user='root', passwd='', port=3306, db='hybdb') #1) 多个db
print('连接成功!')
except:
print('something wrong!')
# exit()
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
#----------> ① 创建user表
# 使用 execute() 方法执行 SQL,如果表存在则删除
rowcount = cursor.execute("DROP TABLE IF EXISTS `user`")
print("删除表成功! rowcount=%d" % rowcount) #2) rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。
# 使用预处理语句创建表
sqlCreateTabole="""CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`nam` varchar(255),
`age` int,
`des` varchar(255),
PRIMARY KEY (`id`))""" #3) 注意3个引号的用法
cursor.execute(sqlCreateTabole)
#----------> ② 增
sqlInsert="INSERT INTO `user` VALUES (null, '张三', 21, '张三家住北京')"
try:
# 执行sql语句
cursor.execute(sqlInsert)
# 提交到数据库执行
db.commit() #4) 默认就开启了事务,所以一定要commit!
print('数据插入成功!')
except:
# 如果发生错误则回滚
db.rollback()
print('数据插入错误!')
sqlInsert2="INSERT INTO `user` VALUES (null, '李四', 22, '李四家住天津')"
sqlInsert3="INSERT INTO `user` VALUES (null, '王五', 23, '王五家住河北')"
try:
# 执行sql语句
cursor.execute(sqlInsert2)
cursor.execute(sqlInsert3)
db.commit()
print('数据插入成功!')
except:
db.rollback()
print('数据插入错误!')
#----------> ③ 查
sqlSelect="SELECT * FROM `user` \
WHERE `id` > %s" % 1 #5) 注意这个反斜杠的用法 #6) 注意 % 1 的用法
cursor.execute(sqlSelect)
rowList = cursor.fetchall() #7) fetchone()方法获取下一个查询结果集,结果集是一个对象。而fetchall():接收全部的返回结果行
for row in rowList:
id = row[0]
nam = row[1]
age = row[2]
des = row[3]
print("id=%d, name=%s, age=%d, des=%s" % \
(id, nam, age, des) \
) #8) 注意这个括号不能少!
# 关闭数据库连接
db.close()
上面这75行代码,如果你能自己完整敲出来,那对数据库的增删改查以及事务处理都没有问题了,也就是说Python的Mysql数据库操作,这一篇文章就够了!下面我再把代码中的8个细节点标注出来,你要细看这8点:
![](https://img-blog.csdnimg.cn/img_convert/4c3958ed660d4c17bd9b7ec61e4e7465.png)
Hyb✈✈✈本文结束,谢谢浏览!