How to use python 3.5.1 with a MySQL database

岁酱吖の 提交于 2019-11-28 18:58:13
S0H31L

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

  • 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!

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

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

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

  3. Copy and run the following 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()

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

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/

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

Try this link: MySQL - Downloads - Connector - Python

From Select Platform, select the platform independent an download MySQLconnector.

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

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

python setup.py install`

Use the mysqlclient library. Install with: pip install mysqlclient

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

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

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

fizux

In Windows, I used:

pip3 install pymysql

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

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

pip install 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.

Hope that will help you !!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!