I have a problem while data logging in a single table in SQL DB. We are not able to do it. When we are running the python code in raspberry pi we are getting the following error msg.
Program:
Expand|Select|Wrap|Line Numbers
- import serial
- import time
- import MySQLdb as mdb
- arduino = serial.Serial("/dev/ttyACM0")
- arduino.baudrate=9600
- data = arduino.readline()
- time.sleep(1)
- data = arduino.readline()
- pieces = data.split("\t")
- temperature = pieces[0]
- humidity = pieces[1]
- con = mdb.connect('localhost', 'root', '12345678', 'testdb');
- with con:
- cursor = connection.cursor(prepared=True)
- sql_insert_query = """ INSERT INTO TempDB (temperature, humidity) VALUES (?,?)"""
- insert_tuple = (temperature, humidity)
- cursor.execute(sql_insert_query, insert_tuple)
- connection.commit()
Traceback (most recent call last):
File "./insertDB.py", line 20, in <module>
cursor = connection.cursor(prepared=True)
NameError: name 'connection' is not defined