MySQL package for python 3.7

后端 未结 4 1095
不知归路
不知归路 2021-02-08 10:23

I am in the need of saving data into a MySQL database, my problem is I can\'t find the package...

Solution explored:

  • Oracle website: https://dev.mysql.c

相关标签:
4条回答
  • 2021-02-08 10:35

    There are two ways to install MySQL connector. The second way is preferred.

    1. MySQL Installer

    This is Oracle's product installer. The problem is that it is outdated. It is only aware of Python version 3.6, and nothing newer. To install for a newer version of Python, use option 2.

    2. Python package manager, pip

    The Python package manager comes with Python, called pip. It downloads the package from the PyPI repository and installs it in an automatic location based on what version of Python (or what virtual copy) you use to install it. The package that you want is mysql-connector-python. In fact, the official documentation says this is the recommended method for installing the MySQL Connector.

    For example, on Windows, open up the Command Prompt (cmd.exe) by searching in the Start Menu. Navigate to the directory where pip is installed. Or make sure that pip's directory is included in the $PATH variable (which you can edit by searching for "PATH" in the Start Menu).

    The command prompt will show you this:

    PS C:\Users\Ryan> 
    

    except instead of my username, it'll show your username, or the path you navigated to after you found pip, like "C:\blah\blah". Then use this command...

    Input:

    pip install mysql-connector-python
    

    It downloads and installs it.

    Results:

    PS C:\Users\Ryan> pip install mysql-connector-python
    Collecting mysql-connector-python
    Downloading https://files.pythonhosted.org/packages/2d/65/3fc902c0f7635912800c6b935313b99b9d4426419ef7ba04f76231b24923/mysql_connector_python-8.0.12-py2.py3-none-any.whl (300kB)
        100% |████████████████████████████████| 307kB 1.1MB/s
    Collecting protobuf>=3.0.0 (from mysql-connector-python)
    Downloading https://files.pythonhosted.org/packages/77/78/a7f1ce761e2c738e209857175cd4f90a8562d1bde32868a8cd5290d58926/protobuf-3.6.1-py2.py3-none-any.whl (390kB)
        100% |████████████████████████████████| 399kB 1.8MB/s
    Requirement already satisfied: setuptools in c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages (from protobuf>=3.0.0->mysql-connector-python) (40.4.3)
    Collecting six>=1.9 (from protobuf>=3.0.0->mysql-connector-python)
    Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
    Installing collected packages: six, protobuf, mysql-connector-python
    Successfully installed mysql-connector-python-8.0.12 protobuf-3.6.1 six-1.11.0
    

    Afterward, you can make sure it is installed by displaying all packages that have been installed by you (not including standard libraries, which come with Python):

    Input:

    PS C:\Users\Ryan> pip list
    

    Results:

    Package                Version
    ---------------------- -------
    mysql-connector-python 8.0.12
    pip                    18.0
    protobuf               3.6.1
    setuptools             40.4.3
    six                    1.11.0
    

    To check if you installed it to the right version of Python, use pip -V. To make sure you installed it to the right copy (virtual or original), look at the file path where the package was installed:

    PS C:\Users\Ryan> pip -V
    

    Results:

    pip 18.0 from c:\users\ryan\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7)
    

    For example, in my system, it shows python37 in the folder path, so it installed it to the original Python 3.7.0 installation I have, instead of an older version or virtual environment (conda or virtualenv, etc).

    To check the version using the Python executable, instead of pip:

    PS C:\Users\Ryan> py -V
    Python 3.7.0
    

    If you need to install it to an older or newer Python version than the default installation, insert the version number as an option (aka. "switch") in the command, using -. For example, to select version 3.6:

    py -3.6 -m pip install mysql-connector-python
    

    The - works on both Windows and Unix-like OSs.

    0 讨论(0)
  • 2021-02-08 10:46

    mysqlclient supports python3.7 officially, you can find it here :

    https://pypi.python.org/pypi/mysqlclient

    1)you can download , PyMySQL 0.9.2
    2)extract & copy the folder pymysql into the python Lib folder
    3)and for connection you can do like this(make a file for example freeman.py):

    #!/usr/bin/env python
    
    import pymysql
    
    conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='freemanDB')
    
    cur = conn.cursor()
    cur.execute("SELECT * FROM users")
    
    print(cur.description)
    print()
    
    for row in cur:
        print(row)
    
    cur.close()
    conn.close()
    
    0 讨论(0)
  • 2021-02-08 10:51
    pip install mysql-connector-python
    

    This command on cmd will solve the problem. Run cmd as administrator if in any case you face problem.

    0 讨论(0)
  • 2021-02-08 10:52

    You must install flask_mysqldb package in order to use MySQL

    pip install flask_mysqldb
    

    To use it:

    from project import app
    
    from flask_mysqldb import MySQL
    

    Config MySQL:

    app.config['MYSQL_HOST'] = 'localhost'
    
    app.config['MYSQL_USER'] = 'root'
    
    app.config['MYSQL_PASSWORD'] = 'yourpassword'
    
    app.config['MYSQL_DB'] = 'mydbfile'
    
    app.config['MYSQL_CURSORCLASS'] = 'MyCursor'
    
    0 讨论(0)
提交回复
热议问题