Can't get mySQL.connector to load / work?
I am trying to connect my raspberry pi to a MySQL database, unfortunately I cannot connect it. Mistake:
pi@raspberrypi:~ $ python HexCodeImporter2.py
Traceback (most recent call last):
File "HexCodeImporter2.py", line 1, in <module>
import mySQL.connector
ImportError: No module named mySQL.connector
The code that pi uses to send data to the server is below:
import mySQL.connector
import serial
ser = serial.Serial('/dev/ttyACM0',9600)
s = [0]
while True:
read_serial = ser.readline()
print read_serial
con = mySQL.connector.connect(user="*******",password="********",host="sql8.freemysqlhosting.net",port="3306",database="CheckIn_System")
c = conn.cursor()
c.execute("insert into CheckIn_System values(read_serial,20170404132401)")
I tried to load mySQL.connector but no luck. How can i do this?
source to share
I believe the preferred way to connect to MySQL database from Python is with MySQLdb
If MySQLdb isn't installed, don't worry about installing it quickly.
- Windows users can get it here: MySQL for Python Installer
- Linux users you can get Linux Install and install it manually, or use your package manager and search for python-mysql
Any write code as follows
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="username", # your username
passwd="password", # your password
db="database_name") # name of the data base
cur = db.cursor()
cur.execute("SELECT * FROM YOUR_TABLE_NAME")
cur.close() # close the cursor
db.close () # close the connection
source to share
I don't know python, but something that happens to me is that you are not calling the correct variable name.
import mySQL.connector
should be:
import con
as "con" is what you called the variable containing the mysql connection data.
You should also move away from the old standard mysql, at least mysqli (improved), if not pdo. https://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059
source to share