Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use python 3.5.1 with a MySQL database

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-winx64 here.

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.

Thanks for any help.

like image 977
Awa Melvine Avatar asked Feb 28 '16 15:02

Awa Melvine


People also ask

Can we use Python and MySQL together?

MySQL server will provide all the services required for handling your database. Once the server is up and running, you can connect your Python application with it using MySQL Connector/Python.


2 Answers

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 :)

like image 162
MasoodRehman Avatar answered Oct 13 '22 08:10

MasoodRehman


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`
like image 41
majid zareei Avatar answered Oct 13 '22 08:10

majid zareei