Python3 MySQL 数据库连接 - PyMySQL 驱动
https://www.runoob.com/python3/python3-mysql.html
一、pymysql 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。接下来安装pymysql:
命令1:打开命令行窗口输入:pip install pymysql
'pip' 不是内部或外部命令,也不是可运行的程序或批处理文件。
将python的安装路径添加到系统环境变量中
D:\python\python3.8.5
D:\python\Scripts
如果未能下载,请看命令2;
命令2:打开命令行窗口输入:python -m pip install pymysql
Collecting pymysql
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl
ERROR: Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl (Caused by ProxyError('Cannot connect to proxy.', timeout('timed out')))
报了这些错误,随后百度,根据该博客内容使用了命令3:
设置pip源:https://www.cnblogs.com/aimed/p/10178048.html
命令3:打开命令行窗口输入:python -m pip install pymysql -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting pymysql
Downloading http://mirrors.aliyun.com/pypi/packages/1a/ea/dd9c81e2d85efd03cfbf808736dd055bd9ea1a78aea9968888b1055c3263/PyMySQL-0.10.1-py2.py3-none-any.whl (47 kB)
|████████████████████████████████| 47 kB ...
Installing collected packages: pymysql
Successfully installed pymysql-0.10.1
WARNING: You are using pip version 20.1.1; however, version 20.3 is available.
You should consider upgrading via the 'D:\python\python3.8.5\python.exe -m pip install --upgrade pip' command.
实现了pymysql的下载,但在最后有一个警告,大意是叫我们更新pip,那我们就更新咯:
WARNING: The scripts pip.exe, pip3.8.exe and pip3.exe are installed in 'D:\python\python3.8.5\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
**命令4:**更新pip:python -m pip install --upgrade pip 或
python -m pip install --upgrade pip -i https://pypi.douban.com/simple
Collecting pip
Downloading pip-20.3-py2.py3-none-any.whl (1.5 MB)
|████████████████████████████████| 1.5 MB 252 kB/s
Installing collected packages: pip
Attempting uninstall: pip
Found existing installation: pip 20.1.1
Uninstalling pip-20.1.1:
Successfully uninstalled pip-20.1.1
WARNING: The scripts pip.exe, pip3.8.exe and pip3.exe are installed in 'D:\python\python3.8.5\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-20.3
第一次执行该命令的时候没有成功,再次执行的时候就成功了,未知原因
二、连接数据库:python连接数据库很简单,主要连接代码如下,具体内容可看文章开头的链接:
#!/usr/bin/python3
import pymysql
# 打开数据库连接
db = pymysql.connect("localhost","testuser","test123","TESTDB" )
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL 查询
cursor.execute("SELECT VERSION()")
# 使用 fetchone() 方法获取单条数据.
data = cursor.fetchone()
print ("Database version : %s " % data)
# 关闭数据库连接
db.close()
三、最后附几个自己查资料过程中的链接:
1、Python3连接MySQL
2、python3连接mysql8.0:
3、mysql-8.0 安装教程(自定义配置文件,密码方式已修改)
4、解决"pip Fatal error in launcher: Unable to create process using … "的错误
5、python使用pip安装模块出错 Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None))
6、python中安装包出现Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None))…………
7、PyCharm安装第三方包requests报错:Try to run this command from the system terminal. Make sure that you use the