参考地址:http://www.runoob.com/python3/python3-mysql.html
以前的代码是python2.7开发的,Python2中则使用mysqldb,现在准备用python3,则用不了mysqldb组件;PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。
1、Windows下安装PyMySQL安装
linux下环境安装比较方便,直接pip install PyMySQL即可,但是windows下面就比较麻烦一些。
先要准备好git cmd环境,然后打开git cmd,进行安装
$ git clone https://github.com/PyMySQL/PyMySQL $ cd PyMySQL/ $ python3 setup.py install
执行过程如下:
C:\Users\laobanzhang>git clone https://github.com/PyMySQL/PyMySQL Cloning into 'PyMySQL'... remote: Counting objects: 3785, done. remote: Compressing objects: 100% (28/28), done. remote: Total 3785 (delta 13), reused 15 (delta 6), pack-reused 3751 Receiving objects: 100% (3785/3785), 954.37 KiB | 294.00 KiB/s, done. Resolving deltas: 100% (2502/2502), done. C:\Users\laobanzhang>cd PyMySQL C:\Users\laobanzhang\PyMySQL>python3 setup.py install 'python3' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 C:\Users\laobanzhang\PyMySQL>python Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> exit Use exit() or Ctrl-Z plus Return to exit >>> exit() C:\Users\laobanzhang\PyMySQL>python setup.py install running install running bdist_egg running egg_info creating PyMySQL.egg-info writing PyMySQL.egg-info\PKG-INFO writing dependency_links to PyMySQL.egg-info\dependency_links.txt writing top-level names to PyMySQL.egg-info\top_level.txt writing manifest file 'PyMySQL.egg-info\SOURCES.txt' reading manifest file 'PyMySQL.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' writing manifest file 'PyMySQL.egg-info\SOURCES.txt' installing library code to build\bdist.win-amd64\egg running install_lib running build_py creating build creating build\lib creating build\lib\pymysql copying pymysql\charset.py -> build\lib\pymysql copying pymysql\connections.py -> build\lib\pymysql copying pymysql\converters.py -> build\lib\pymysql copying pymysql\cursors.py -> build\lib\pymysql copying pymysql\err.py -> build\lib\pymysql copying pymysql\optionfile.py -> build\lib\pymysql copying pymysql\times.py -> build\lib\pymysql copying pymysql\util.py -> build\lib\pymysql copying pymysql\_compat.py -> build\lib\pymysql copying pymysql\_socketio.py -> build\lib\pymysql copying pymysql\__init__.py -> build\lib\pymysql creating build\lib\pymysql\constants copying pymysql\constants\CLIENT.py -> build\lib\pymysql\constants copying pymysql\constants\COMMAND.py -> build\lib\pymysql\constants copying pymysql\constants\CR.py -> build\lib\pymysql\constants copying pymysql\constants\ER.py -> build\lib\pymysql\constants copying pymysql\constants\FIELD_TYPE.py -> build\lib\pymysql\constants copying pymysql\constants\FLAG.py -> build\lib\pymysql\constants copying pymysql\constants\SERVER_STATUS.py -> build\lib\pymysql\constants copying pymysql\constants\__init__.py -> build\lib\pymysql\constants creating build\lib\pymysql\tests copying pymysql\tests\base.py -> build\lib\pymysql\tests copying pymysql\tests\test_basic.py -> build\lib\pymysql\tests copying pymysql\tests\test_connection.py -> build\lib\pymysql\tests copying pymysql\tests\test_converters.py -> build\lib\pymysql\tests copying pymysql\tests\test_cursor.py -> build\lib\pymysql\tests copying pymysql\tests\test_DictCursor.py -> build\lib\pymysql\tests copying pymysql\tests\test_err.py -> build\lib\pymysql\tests copying pymysql\tests\test_issues.py -> build\lib\pymysql\tests copying pymysql\tests\test_load_local.py -> build\lib\pymysql\tests copying pymysql\tests\test_nextset.py -> build\lib\pymysql\tests copying pymysql\tests\test_optionfile.py -> build\lib\pymysql\tests copying pymysql\tests\test_SSCursor.py -> build\lib\pymysql\tests copying pymysql\tests\__init__.py -> build\lib\pymysql\tests creating build\lib\pymysql\tests\thirdparty copying pymysql\tests\thirdparty\__init__.py -> build\lib\pymysql\tests\thirdpar ty creating build\lib\pymysql\tests\thirdparty\test_MySQLdb copying pymysql\tests\thirdparty\test_MySQLdb\capabilities.py -> build\lib\pymys ql\tests\thirdparty\test_MySQLdb copying pymysql\tests\thirdparty\test_MySQLdb\dbapi20.py -> build\lib\pymysql\te sts\thirdparty\test_MySQLdb copying pymysql\tests\thirdparty\test_MySQLdb\test_MySQLdb_capabilities.py -> bu ild\lib\pymysql\tests\thirdparty\test_MySQLdb copying pymysql\tests\thirdparty\test_MySQLdb\test_MySQLdb_dbapi20.py -> build\l ib\pymysql\tests\thirdparty\test_MySQLdb copying pymysql\tests\thirdparty\test_MySQLdb\test_MySQLdb_nonstandard.py -> bui ld\lib\pymysql\tests\thirdparty\test_MySQLdb copying pymysql\tests\thirdparty\test_MySQLdb\__init__.py -> build\lib\pymysql\t ests\thirdparty\test_MySQLdb creating build\bdist.win-amd64 creating build\bdist.win-amd64\egg creating build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\charset.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\connections.py -> build\bdist.win-amd64\egg\pymysql creating build\bdist.win-amd64\egg\pymysql\constants copying build\lib\pymysql\constants\CLIENT.py -> build\bdist.win-amd64\egg\pymys ql\constants copying build\lib\pymysql\constants\COMMAND.py -> build\bdist.win-amd64\egg\pymy sql\constants copying build\lib\pymysql\constants\CR.py -> build\bdist.win-amd64\egg\pymysql\c onstants copying build\lib\pymysql\constants\ER.py -> build\bdist.win-amd64\egg\pymysql\c onstants copying build\lib\pymysql\constants\FIELD_TYPE.py -> build\bdist.win-amd64\egg\p ymysql\constants copying build\lib\pymysql\constants\FLAG.py -> build\bdist.win-amd64\egg\pymysql \constants copying build\lib\pymysql\constants\SERVER_STATUS.py -> build\bdist.win-amd64\eg g\pymysql\constants copying build\lib\pymysql\constants\__init__.py -> build\bdist.win-amd64\egg\pym ysql\constants copying build\lib\pymysql\converters.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\cursors.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\err.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\optionfile.py -> build\bdist.win-amd64\egg\pymysql creating build\bdist.win-amd64\egg\pymysql\tests copying build\lib\pymysql\tests\base.py -> build\bdist.win-amd64\egg\pymysql\tes ts copying build\lib\pymysql\tests\test_basic.py -> build\bdist.win-amd64\egg\pymys ql\tests copying build\lib\pymysql\tests\test_connection.py -> build\bdist.win-amd64\egg\ pymysql\tests copying build\lib\pymysql\tests\test_converters.py -> build\bdist.win-amd64\egg\ pymysql\tests copying build\lib\pymysql\tests\test_cursor.py -> build\bdist.win-amd64\egg\pymy sql\tests copying build\lib\pymysql\tests\test_DictCursor.py -> build\bdist.win-amd64\egg\ pymysql\tests copying build\lib\pymysql\tests\test_err.py -> build\bdist.win-amd64\egg\pymysql \tests copying build\lib\pymysql\tests\test_issues.py -> build\bdist.win-amd64\egg\pymy sql\tests copying build\lib\pymysql\tests\test_load_local.py -> build\bdist.win-amd64\egg\ pymysql\tests copying build\lib\pymysql\tests\test_nextset.py -> build\bdist.win-amd64\egg\pym ysql\tests copying build\lib\pymysql\tests\test_optionfile.py -> build\bdist.win-amd64\egg\ pymysql\tests copying build\lib\pymysql\tests\test_SSCursor.py -> build\bdist.win-amd64\egg\py mysql\tests creating build\bdist.win-amd64\egg\pymysql\tests\thirdparty creating build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\test_MySQLdb\capabilities.py -> build \bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\test_MySQLdb\dbapi20.py -> build\bdis t.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\test_MySQLdb\test_MySQLdb_capabilitie s.py -> build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\test_MySQLdb\test_MySQLdb_dbapi20.py -> build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\test_MySQLdb\test_MySQLdb_nonstandard .py -> build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\test_MySQLdb\__init__.py -> build\bdi st.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb copying build\lib\pymysql\tests\thirdparty\__init__.py -> build\bdist.win-amd64\ egg\pymysql\tests\thirdparty copying build\lib\pymysql\tests\__init__.py -> build\bdist.win-amd64\egg\pymysql \tests copying build\lib\pymysql\times.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\util.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\_compat.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\_socketio.py -> build\bdist.win-amd64\egg\pymysql copying build\lib\pymysql\__init__.py -> build\bdist.win-amd64\egg\pymysql byte-compiling build\bdist.win-amd64\egg\pymysql\charset.py to charset.cpython-3 6.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\connections.py to connections.c python-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\CLIENT.py to CLIENT.c python-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\COMMAND.py to COMMAND .cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\CR.py to CR.cpython-3 6.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\ER.py to ER.cpython-3 6.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\FIELD_TYPE.py to FIEL D_TYPE.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\FLAG.py to FLAG.cpyth on-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\SERVER_STATUS.py to S ERVER_STATUS.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\constants\__init__.py to __init __.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\converters.py to converters.cpy thon-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\cursors.py to cursors.cpython-3 6.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\err.py to err.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\optionfile.py to optionfile.cpy thon-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\base.py to base.cpython-3 6.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_basic.py to test_bas ic.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_connection.py to tes t_connection.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_converters.py to tes t_converters.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_cursor.py to test_cu rsor.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_DictCursor.py to tes t_DictCursor.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_err.py to test_err.c python-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_issues.py to test_is sues.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_load_local.py to tes t_load_local.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_nextset.py to test_n extset.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_optionfile.py to tes t_optionfile.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\test_SSCursor.py to test_ SSCursor.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb\c apabilities.py to capabilities.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb\d bapi20.py to dbapi20.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb\t est_MySQLdb_capabilities.py to test_MySQLdb_capabilities.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb\t est_MySQLdb_dbapi20.py to test_MySQLdb_dbapi20.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb\t est_MySQLdb_nonstandard.py to test_MySQLdb_nonstandard.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\test_MySQLdb\_ _init__.py to __init__.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\thirdparty\__init__.py to __init__.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\tests\__init__.py to __init__.c python-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\times.py to times.cpython-36.py c byte-compiling build\bdist.win-amd64\egg\pymysql\util.py to util.cpython-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\_compat.py to _compat.cpython-3 6.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\_socketio.py to _socketio.cpyth on-36.pyc byte-compiling build\bdist.win-amd64\egg\pymysql\__init__.py to __init__.cpython -36.pyc creating build\bdist.win-amd64\egg\EGG-INFO copying PyMySQL.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO copying PyMySQL.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO copying PyMySQL.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-I NFO copying PyMySQL.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO zip_safe flag not set; analyzing archive contents... pymysql.tests.__pycache__.base.cpython-36: module references __file__ pymysql.tests.__pycache__.test_load_local.cpython-36: module references __file__ creating dist creating 'dist\PyMySQL-0.8.0-py3.6.egg' and adding 'build\bdist.win-amd64\egg' t o it removing 'build\bdist.win-amd64\egg' (and everything under it) Processing PyMySQL-0.8.0-py3.6.egg creating c:\users\laobanzhang\appdata\local\programs\python\python36\lib\site-pa ckages\PyMySQL-0.8.0-py3.6.egg Extracting PyMySQL-0.8.0-py3.6.egg to c:\users\laobanzhang\appdata\local\program s\python\python36\lib\site-packages Adding PyMySQL 0.8.0 to easy-install.pth file Installed c:\users\laobanzhang\appdata\local\programs\python\python36\lib\site-p ackages\pymysql-0.8.0-py3.6.egg Processing dependencies for PyMySQL==0.8.0 Finished processing dependencies for PyMySQL==0.8.0 C:\Users\laobanzhang\PyMySQL>
2、如果要定制版本号,可以使用cual安装
$ # X.X 为 PyMySQL 的版本号 $ curl -L https://github.com/PyMySQL/PyMySQL/tarball/pymysql-X.X | tar xz $ cd PyMySQL* $ python3 setup.py install $ # 现在你可以删除 PyMySQL* 目录
3、连接实例实例代码:
#!/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()