Python3安装Mysql驱动包PyMySQL

版权声明:原创文章,转载请注明出处! https://blog.csdn.net/L_15156024189/article/details/84888980

1、下载PyMySQL包

登录https://pypi.org/网站,输入pymysql,搜索PyMySQL包,如图:

搜索后结果如图:

 

点击搜索结果PyMySQL 0.9.2后,如图:

然后点击Download files,页面如图:

 点击whl格式的文件下载。

2、安装PyMySQL包

在Python安装目录下,新建一个whl目录(以后可以专门用来放置whl格式的python包),将刚下载的whl文件放入该目录下,如图:

进入Python安装目录下的Scripts下,按下Shift后,右键选择【在此处打开命令窗口】,弹出命令窗口,并在窗口中输入安装命令,如图:

注:可以直接在命令窗口中执行pip install pymysql来安装。

3、使用PyMySQL包连接数据库

MySQL具体如下:

 下面我们通过Python代码,连接springcloud数据库,查询organizations表中的name字段,Python代码如下:

import pymysql

# 创建连接
conn = pymysql.connect(host="localhost",
                       port=3306,
                       user="root",
                       passwd="root",
                       db="springcloud")
# 创建游标
cursor = conn.cursor()
# 执行mysql语句
cursor.execute("select name from organizations")

# 获取所有的执行结果
res = cursor.fetchall()
# 打印获取到的执行结果
print(res)

# 提交要执行的mysql指令
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

 程序运行结果:

(('HR-PowerSuite',), ('customer-crm-co',))

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/L_15156024189/article/details/84888980