Posts: 5
Threads: 3
Joined: Jun 2020
Jun-23-2020, 04:04 PM
(This post was last modified: Jun-23-2020, 04:25 PM by buran.)
Dear All,
I have a problem while data logging in the single DB table in SQL server. We are not able to do it. When we are running the python code in raspberry pi we are getting the following error msg.
ERROR MESSAGE
Error: File "./insertDB.py", line 19
//cursor = con.cursor(prepared=True)
^
SyntaxError: invalid syntax
Please anyone help me out.
Py Code:
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]
#This is a comment
con = mdb.connect('localhost', 'root', '12345678', 'testdb');
with con:
//cursor = con.cursor(prepared=True)
sql_insert_query = """ INSERT INTO TempDB (temperature, humidity) VALUES (%s,%s)"""
insert_tuple = (temperature, humidity)
cursor.execute(sql_insert_query, insert_tuple)
con.commit()
Posts: 8,156
Threads: 160
Joined: Sep 2016
What do you expect // is doing? line 21 is not a valid coce. Just remove //
Posts: 5
Threads: 3
Joined: Jun 2020
Jun-23-2020, 04:30 PM
(This post was last modified: Jun-23-2020, 04:51 PM by buran.)
(Jun-23-2020, 04:27 PM)buran Wrote: What do you expect // is doing? line 21 is not a valid coce. Just remove //
Dear Friend,
Thanks for your reply. But if I remove, I am getting following message
Error Message
Traceback (most recent call last):
File "./insertDB.py", line 21, in <module>
cursor = con.cursor(prepared=True)
TypeError: cursor() got an unexpected keyword argument 'prepared'
Posts: 8,156
Threads: 160
Joined: Sep 2016
Jun-23-2020, 04:51 PM
(This post was last modified: Jun-23-2020, 04:53 PM by buran.)
I didn't use MySQLdb, but in the docs I don't see example of using prepared statements.
I would recommend to use official mysql connector https://dev.mysql.com/doc/connector-python/en/ instead of MySQLdb
here is the docs about MySQLCursorPrepared class:
https://dev.mysql.com/doc/connector-pyth...pared.html
Posts: 1,144
Threads: 114
Joined: Sep 2019
I have not ever been able to get the prepared statement to work. Remove the prepared part and it should work. The tuple part, should take care of the injection protection.
Posts: 8,156
Threads: 160
Joined: Sep 2016
Jun-23-2020, 05:00 PM
(This post was last modified: Jun-23-2020, 05:00 PM by buran.)
Also it looks like you are using python2 - mysql-python package is targeting python2 and is not updated since 2014. Python2 support has ended. use python3
Posts: 1,144
Threads: 114
Joined: Sep 2019
Here is a piece of code from one of my test projects modified. What a query would look like using pymysql
#! /usr/bin/env python3
import pymysql
host = 'localhost'
user = 'root'
password = '12345678'
database = 'testdb'
port = 3306
try:
conn = pymysql.connect(host, user, password, database, port)
values = (temperature, humidity)
query = 'insert into recipes (temperature, humidity) values (%s, %s)'
with conn.cursor() as cursor:
cursor.execute(query, values)
cursor.connection.commit()
except (pymysql.OperationalError, pymysql.ProgrammingError) as error:
print(error)
Posts: 5
Threads: 3
Joined: Jun 2020
Jun-23-2020, 05:17 PM
(This post was last modified: Jun-23-2020, 05:22 PM by buran.)
(Jun-23-2020, 05:00 PM)buran Wrote: Also it looks like you are using python2 - mysql-python package is targeting python2 and is not updated since 2014. Python2 support has ended. use python3
Yes you are right. If I run with python3
I am getting below error
Traceback (most recent call last):
File "./insertDB.py", line 3, in <module>
import MySQLdb as mdb
ModuleNotFoundError: No module named 'MySQLdb'
How to change this code to Python3 ?
Posts: 8,156
Threads: 160
Joined: Sep 2016
Jun-23-2020, 05:23 PM
(This post was last modified: Jun-23-2020, 05:23 PM by buran.)
First of all - start using BBcode, you have been advised to do so twice
Install the "official" mysql connector as advised in my post # 4. be careful to install it for python3 - obviously you have both python2 and python3. mysql-python supports only python2 and is outdated.
full docs and examples - in the link in my previous post
|