How to use python 3.5.1 with a MySQL database

58,709

Solution 1

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!

Solution 2

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

Solution 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`

Solution 4

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.

Solution 5

In Windows, I used:

pip3 install pymysql
Share:
58,709
Awa Melvine
Author by

Awa Melvine

Updated on July 09, 2022

Comments

  • Awa Melvine
    Awa Melvine almost 2 years

    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.

    • Gustavo Bezerra
      Gustavo Bezerra about 8 years
      Try pymysql. It is compatible with Python 3. You can install it with pip: pip install pymysql.
  • RaGa__M
    RaGa__M almost 8 years
    just noted import "pymysql.cursors", and i am getting WinError 10061 the Mysql server:) don't we have any commands like apt get to get the whole thing including the server on windows
  • Yega
    Yega over 7 years
    I did $ pip install PyMySQL and it worked like a charm (see pypi )
  • Stephen Rauch
    Stephen Rauch about 7 years
    This is an identical answer to one above in the comments.
  • user3450049
    user3450049 about 7 years
    Brilliant! This worked for me on Windows with python3.5 when I couldn't find any other way to get mysql package (dependency for sqlalchemy)
  • chjortlund
    chjortlund about 7 years
    To complete the insert remember to do a conn.commit()
  • Richard
    Richard about 7 years
    What worked for me on Ubuntu 16 pip3 install pymysql and removed the line import pymysql.cursor.
  • romor
    romor almost 7 years
    I had to start the power shell as Administrator to be able to install the package to site-packages. It worked then using Python 3.6.
  • Mordechai
    Mordechai almost 7 years
    shouldn't it be import pymysql.cursors?
  • jftuga
    jftuga over 6 years
    This also works on Windows 10 with Python 3.6.3 and MariaDB 10.2.9.
  • Soheil
    Soheil over 6 years
    @Mordechai I have imported it