How to use python 3.5.1 with a MySQL database
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.
Download driver (PyMySQL-0.7.9.tar.gz (md5)) from here
Extract and copy the folder pymysql into the python Lib folder e.g (C:\Users\MyUsername\AppData\Local\Programs\Python\Python35-32\Lib)
- 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
Awa Melvine
Updated on July 09, 2022Comments
-
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 about 8 yearsTry
pymysql
. It is compatible with Python 3. You can install it with pip:pip install pymysql
.
-
-
RaGa__M almost 8 yearsjust 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 over 7 yearsI did
$ pip install PyMySQL
and it worked like a charm (see pypi ) -
Stephen Rauch about 7 yearsThis is an identical answer to one above in the comments.
-
user3450049 about 7 yearsBrilliant! 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 about 7 yearsTo complete the insert remember to do a
conn.commit()
-
Richard about 7 yearsWhat worked for me on Ubuntu 16
pip3 install pymysql
and removed the lineimport pymysql.cursor
. -
romor almost 7 yearsI 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 almost 7 yearsshouldn't it be import pymysql.cursors?
-
jftuga over 6 yearsThis also works on Windows 10 with Python 3.6.3 and MariaDB 10.2.9.
-
Soheil over 6 years@Mordechai I have imported it