I have a problem so a connection a python to database local of MySQLdb.
Message of error is:
import MYSQL ImportError:No module named MYSQLdb. This is a script in python 3.4 to connection my database local MYSQL.
import MYSQLdb conn=MYSQLdb.connect (host ="localhost", user="root" , passwd="abcd",db="nomi") cursore=conn.cursor() name=input("Write your name : ") x="insert into user (name) values ('%s')"%(name) cursore.excute(x) connet.comit() PS: Can you help me please?
8 Answers
sudo apt install libmysqlclient-dev then
for python 2.x:
pip install mysql-pythonfor python 3.x:
pip install mysqlclientorpip3 install mysqlclient
You have to install the module with:
sudo apt-get install python-mysqldb Reference: This thread on SO
3Unfortunately MySQLdb does not support Python 3.
You basically have two options:
Run your script using python2.7, that way you won't need to change the MySQL module. The downside is that you'll probably have to convert some code to python2.x.
Look for python3.x supported modules as explained here: Python 3.4.0 with MySQL database.
I got this issue solved by the following line
pip install pymysql Also updated the MySql configuration line to
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://.............' 1As far as I know, MySQLdb does not support Python 3. From the page '[MySQL-python]'1 updated on 2013-06-08:
* Python versions 2.4-2.7; Python 3 support coming soon. mysqlclient-python () claims support for python 3 but I have no experience with that product. Personally, I use pyodbc (also requires libmyodbc) which looks very similar from a programming point of view.
The answer by @technodivesh worked for me in debian 9 and python 3.5. But with a little modification.
Instead of
sudo apt install libmysqlclient-dev I used:
sudo apt install default-libmysqlclient-dev It was suggested here
The first command returned me-
E: Package 'libmysqlclient-dev' has no installation candidate
[I would have added this as a comment to his answer. But, I do not have the required reputation to make comments.]
For Python3 its
sudo apt-get install python-mysqldb Other commands I ran that may or may not be required (the command above ultimately solved everything for me)
#this was for a new machine set up. one or two of the below failed sudo apt install python3-pip pip3 install mysqlclient pip3 install mysql-connector pip3 install mysql-connector-python This is very easy. You have to write just like this:
import Mysql.connector mydb = Mysql.connector.connect( host="127.0.0.1", user="root", passwd="shriram@11" ) mycursor = mydb.cursor() mycursor.execute("CREATE DATABASE mydatabase") This code will run correctly without showing any module error