zoukankan      html  css  js  c++  java
  • Python安装MySQL驱动

    在Python2.x中访问MySQL数据库可以使用第三方库MySQL-Python(即MySQLdb),但是MySQLdb不支持Python3.x。在Python3.x中,可以使用另一个第三方库MySQLclient作为替代,它是基于MySQL-Python开发的,提供了和前者几乎相同的接口。因此,在两个Python版本下,可以使用相同的代码访问MySQL。

    Python2.x中

    pip install -i https://pypi.douban.com/simple/ mysql-python
    

    Windows下如果出现错误:

    _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h': No such file or directory
    

    提示缺少C++的相关环境,到 http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python 下载对应版本的包。
    下载完成之后安装:

    F:Downloads>workon test2
    (test2) F:Downloads>pip install MySQL_python-1.2.5-cp27-none-win_amd64.whl
    DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
    Processing f:Downloadsmysql_python-1.2.5-cp27-none-win_amd64.whl
    Installing collected packages: MySQL-python
    Successfully installed MySQL-python-1.2.5
    
    (test2) F:Downloads>
    

    然后就安装成功了MySQL-python

    Python3.x中

    pip install -i https://pypi.douban.com/simple/ mysqlclient
    

    同样,Windows下如果出现错误,到 http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient 下载对应版本的包即可。
    在这里插入图片描述

    下载之后安装

    F:Downloads>workon Py3_spider
    (Py3_spider) F:Downloads>pip install mysqlclient-1.4.1-cp37-cp37m-win_amd64.whl
    Processing f:Downloadsmysqlclient-1.4.1-cp37-cp37m-win_amd64.whl
    Installing collected packages: mysqlclient
    Successfully installed mysqlclient-1.4.1
    
    (Py3_spider) F:Downloads>
    

    linux下安装失败解决办法:

    ubuntu:

    sudo apt-get install libmysqlclient-dev
    

    centos:

    sudo yum install python-devel mysql-devel
    

    附:
    常见 MySQL 驱动介绍:

    1. MySQL-python :也就是 MySQLdb 。是对 C 语言操作 MySQL 数据库的一个简单封装。遵循了 Python DB API v2 。但是只支持 Python2 ,目前还不支持 Python3 。
    2. mysqlclient :是 MySQL-python 的另外一个分支。支持 Python3 并且修复了一些 bug 。
    3. pymysql :纯 Python 实现的一个驱动。因为是纯 Python 编写的,因此执行效率不如 MySQL-python 。并且也因为是纯 Python 编写的,因此可以和 Python 代码无缝衔接。
    4. MySQL Connector/Python : MySQL 官方推出的使用纯 Python 连接 MySQL 的驱动。因为是纯 Python 开发的。效率不高。
  • 相关阅读:
    Tensorflowlite移植ARM平台iMX6
    人生信条集
    浅谈聚类
    常用距离度量方法大全
    sklearn学习小结
    SpringBoot 2.x版本+MultipartFile设置指定文件上传大小
    SpringBoot无法访问webapp目录下的文件
    idea搜索不到任何插件
    Caused by: org.springframework.data.mapping.PropertyReferenceException: No property id found for type Users!
    Annotation-specified bean name 'userDaoImpl' for bean class [***] conflicts with existing, non-compatible bean definition of same name and class [***]
  • 原文地址:https://www.cnblogs.com/onefine/p/10499359.html
Copyright © 2011-2022 走看看