Python操作MySQL数据库

来源:互联网 发布:java小游戏开发实例 编辑:程序博客网 时间:2024/06/05 17:57

安装python-mysqldb

    只支持python2.x
    方式一:执行命令
    sudo apt-get install python-mysqldb
    
    方式二:使用pip安装
    pip install mysql-python
    报错:EnvironmentError: mysql_config not found
    执行:sudo apt-get install libmysqld-dev
    重新执行pip命令安装
    
    方式三:使用Pycharm IDE的包管理器,搜索mysql,选择安装
    
    方式四:源码包安装
    python setup.py install


安装mysql-connector

    MySQL官方驱动包,支持python3.x

    pip install mysql-connector-python


安装mysqlclient

    是对python-mysqldb的封装,支持Python2.x和3.x。建议使用此模块操作mysql数据库。
    pypi地址:https://pypi.python.org/pypi/mysqlclient/1.3.12
    github地址:https://github.com/PyMySQL/mysqlclient-python
    
    Prerequisites
    You may need to install the Python and MySQL development headers and libraries like so:

        sudo apt-get install python-dev libmysqlclient-dev # Debian / Ubuntu
        sudo yum install python-devel mysql-devel # Red Hat / CentOS
        On Windows, there are binary wheel you can install without MySQLConnector/C or MSVC.

    Note on Python 3 : if you are using python3 then you need to install python3-dev using the following command :
        sudo apt-get install python3-dev # debian / Ubuntu
        sudo yum install python3-devel # Red Hat / CentOS
        brew install mysql-connector-c # macOS (Homebrew)

    Install from PyPI
        pip install mysqlclient

    NOTE: Wheels for Windows may be not released with source package. You should pin version in your requirements.txt 
    to avoid trying to install newest source package.

    Install from source
        1.Download source by git clone or zipfile.
        2.Customize site.cfg
        3.python setup.py install

    
    
    
原创粉丝点击