Problem
You want to connect to MySQL using Python.
https://stackoverflow.com/questions/25865270/how-to-install-python-mysqldb-module-using-pip
Many tutorials and post talk about using python-MySQLdb. This did not work for me, as you can see...
The error
sudo apt install python-pip
pip install MySQL-python
sudo apt-get install python-pip python-dev libmysqlclient-dev
Then try again
pip install MySQL-python
..
..the above is all nonsense..
Solution
sudo apt install python3-pip
sudo pip3 install mysql-connector-python
Note "mysql" is there
And to use...
Result
My dboconnect.py code:
<<dbconnect.py>>
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='epicdb',
user='newuser',
password='febr99')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='epicdb',
user='newuser',
password='febr99')
if connection.is_connected():
db_Info = connection.get_server_info()
print("Connected to MySQL Server version ", db_Info)
cursor = connection.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("You're connected to database: ", record)
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
print("MySQL connection is closed")
For more info on using this module
Acknowledgment
https://pynative.com/install-mysql-connector-python/
Comments
Post a Comment