Hugo Til
Hugo Til

Reputation: 31

Cannot get mySQL.connector to download/work?

I am trying to connect my raspberry pi to a MySQL database, unfortunately I cannot get it to connect. The error is:

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 is used on the pi to send the 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 have tried to download the mySQL.connector but with no luck. How could I do this?

Upvotes: 3

Views: 621

Answers (2)

Sagar Damani
Sagar Damani

Reputation: 922

I believe the preferred way to connect to MySQL database from Python is with MySQLdb

If MySQLdb not installed don’t worry it’s a quick install.

Any write code in following way

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

Upvotes: 2

Simon Cooper
Simon Cooper

Reputation: 1594

I don't know python, but something that occurs to me is that you're not calling the correct variable name.

import mySQL.connector

should possibly be:

import con, as "con" is what you've called the variable holding the connection details to mysql.

You should also move away from the old standard mysql to at least mysqli (improved) if not pdo.https://code.tutsplus.com/tutorials/pdo-vs-mysqli-which-should-you-use--net-24059

Upvotes: 0

Related Questions