如何在 MySQL 数据库中使用 python 3.5.1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35684400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:48:56  来源:igfitidea点击:

How to use python 3.5.1 with a MySQL database

pythonmysql-connector

提问by Awa Melvine

I have been trying to use MySQL in a Python project I've been working on. I downloaded the connector: mysql-connector-python-2.1.3-py3.4-winx64here.

我一直在尝试在我一直从事的 Python 项目中使用 MySQL。我下载了连接器:mysql-connector-python-2.1.3-py3.4-winx64here。

I already had Python 3.5.1 installed. When I tried to install the connector, it didn't work because it required python 2.7 instead. I have searched on many sites, even on StackOverflow I couldn't find a solution.

我已经安装了 Python 3.5.1。当我尝试安装连接器时,它不起作用,因为它需要 python 2.7。我在很多网站上搜索过,甚至在 StackOverflow 上也找不到解决方案。

Thanks for any help.

谢谢你的帮助。

回答by S0H31L

I did the steps below with Python 3.5.1 and it works:

我使用 Python 3.5.1 执行了以下步骤,并且可以正常工作:

  • Download driver from here
  • Driver installation in cmd, in this folder Python\Python35\PyMySQL-0.7.4\pymysql

    python setup.py build
    python setup.py install
    
  • Copy folder Python\Python35\PyMySQL-0.7.4\pymysql to Python\Python35\pymysql

  • Sample code in python IDE

    import pymysql
    import pymysql.cursors
    conn= pymysql.connect(host='localhost',user='user',password='user',db='testdb',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
    a=conn.cursor()
    sql='CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'
    a.execute(sql)
    
  • Enjoy It!
  • 这里下载驱动程序
  • 在cmd中安装驱动,在这个文件夹Python\Python35\PyMySQL-0.7.4\pymysql

    python setup.py build
    python setup.py install
    
  • 将文件夹 Python\Python35\PyMySQL-0.7.4\pymysql 复制到 Python\Python35\pymysql

  • python IDE中的示例代码

    import pymysql
    import pymysql.cursors
    conn= pymysql.connect(host='localhost',user='user',password='user',db='testdb',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
    a=conn.cursor()
    sql='CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'
    a.execute(sql)
    
  • 好好享受!

回答by MasoodUrRehman

I am using Python 3.5.2on window 8 pro 64-bitand the following procedure is worked for me.

我在windows 8 pro 64 位上使用Python 3.5.2,以下过程对我有用。

  1. Download driver (PyMySQL-0.7.9.tar.gz (md5)) from here

  2. Extract and copy the folder pymysqlinto the python Lib folder e.g (C:\Users\MyUsername\AppData\Local\Programs\Python\Python35-32\Lib)

  3. Copy and run the following example.py
  1. 这里下载驱动程序 (PyMySQL-0.7.9.tar.gz (md5))

  2. 将文件夹pymysql提取并复制到 python Lib 文件夹中,例如 (C:\Users\MyUsername\AppData\Local\Programs\Python\Python35-32\Lib)

  3. 复制并运行下面的example.py
#!/usr/bin/env python

import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='sandbox')

cur = conn.cursor()
cur.execute("SELECT * FROM users")

print(cur.description)
print()

for row in cur:
    print(row)

cur.close()
conn.close()
#!/usr/bin/env python

import pymysql

conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='sandbox')

cur = conn.cursor()
cur.execute("SELECT * FROM users")

print(cur.description)
print()

for row in cur:
    print(row)

cur.close()
conn.close()

I hope it will work for you as well. Happy coding :)

我希望它也适用于你。快乐编码:)

回答by Alex C.

Visit this web site and you will find a mysqld package that works fine with Python 3 on Windows : http://www.lfd.uci.edu/~gohlke/pythonlibs/

访问这个网站,你会发现一个 mysqld 包,它可以在 Windows 上与 Python 3 一起正常工作:http: //www.lfd.uci.edu/~gohlke/pythonlibs/

Otherwise you can use pymysqlwhich might be slower but works fine with Python 3.

否则,您可以使用pymysql,它可能会更慢,但在 Python 3 中运行良好。

回答by majid zareei

Try this link: MySQL - Downloads - Connector - Python

试试这个链接:MySQL - 下载 - 连接器 - Python

From Select Platform, select the platform independentan download MySQLconnector.

从 中Select Platform,选择platform independent一个下载 MySQLconnector。

After extracting the file go to its directory where setup.pyis located.

解压缩文件后,转到setup.py所在的目录 。

WINDOWS:press shift+ right_clickand open command windows and type:

WINDOWS:shift+ right_click并打开命令窗口并键入:

python setup.py install`

回答by fizux

In Windows, I used:

在 中Windows,我使用了:

pip3 install pymysql

回答by The Aelfinn

Use the mysqlclient library. Install with: pip install mysqlclient

使用mysqlclient 库。安装:pip install mysqlclient

It is a fork of MySQLdb ( which was formerly installed via pip install mysql-python) that supports Python 3.*

它是 MySQLdb(以前通过 安装pip install mysql-python)的一个分支,支持 Python 3.*

This library talks to the MySQL client's C-interface, and is faster than the pure-python pymysqllibray.

这个库与 MySQL 客户端的 C 接口通信,并且比纯 pythonpymysql库更快。

*Note: you will need the mysql-developer tools installed. An easy way to do this on a Mac is to run brew install mysqlto delegate this task to homebrew. If you are on linux, you can install these via the instructions at the mysqlclientgithub page.

*注意:您需要安装 mysql-developer 工具。在 Mac 上执行此操作的一种简单方法是运行brew install mysql以将此任务委托给homebrew。如果您使用的是 linux,则可以通过mysqlclientgithub 页面上的说明安装这些。

回答by Ankit Aggarwal

I was facing mysql database connection problem with Windows, Python 3.5.2 and Django. But finally I resolved it by installing "mysqlclient?1.3.9?cp35?cp35m?win32.whl" from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

我在使用 Windows、Python 3.5.2 和 Django 时遇到了 mysql 数据库连接问题。但最后我通过从http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient安装“ mysqlclient?1.3.9?cp35?cp35m?win32.whl”解决了这个问题

Download the whl file, then enter into same directory in command prompt and run the below command.

下载 whl 文件,然后在命令提示符下进入同一目录并运行以下命令。

pip install mysqlclient-1.3.9-cp35-cp35m-win32.whl

pip 安装 mysqlclient-1.3.9-cp35-cp35m-win32.whl

Note: Python 3.5.2 does not have official support for MySQL yet, so its just un-official binary to over come this problem for now.

注意:Python 3.5.2 还没有对 MySQL 的官方支持,所以现在它只是一个非官方的二进制文件来解决这个问题。

Hope that will help you !!!

希望能帮到你!!!