Error in inserting python variable in mysql table - python

I am working on a raspberry pi project, in which I'm fetching data from plc and storing it into mysql database.
Here is my code:
import minimalmodbus
import serial
import mysql.connector
instrument = minimalmodbus.Instrument('/dev/ttyAMA0',3,mode='rtu')
instrument.serial.baudrate=115200
instrument.serial.parity = serial.PARITY_NONE
instrument.serial.bytesize = 8
instrument.serial.stopbits = 1
instrument.serial.timeout = 0.05
con = mysql.connector.connect(user='root',password='raspberry',host='localhost',
database='Fujiplc')
cursor = con.cursor()
try:
reg_value=instrument.read_register(102)
print reg_value
cursor.execute("insert into Register_Values values(%s)",(reg_value))
print ('One row inserted successfully.')
except IOError:
print("Failed to read from PLC.")
print (cursor.rowcount)
con.commit()
cursor.close()
con.close()
After running this code, I get next error:
Traceback (most recent call last):
File "/home/pi/rpi_to_plc_read.py", line 22, in <module>
cursor.execute("insert into Register_Values values(%d)",(reg_value))
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 477, in execute
stmt = operation % self._process_params(params)
File "/usr/local/lib/python2.7/dist-packages/mysql/connector/cursor.py", line 355, in _process_params
"Failed processing format-parameters; %s" % err)
ProgrammingError: Failed processing format-parameters; argument 2 to map() must support iteration
I have gone through so many solutions but problem couldn't solve.
Please help me.

i think should be.
cursor.execute("insert into Register_Values values(%s)",(reg_value))
con.commit()

Pretty common error in python.
(reg_value) is not a tuple
(reg_value,) is a tuple

Related

Inserting multiple MySQL records using Python. ERROR: "Python 'tuple' cannot be converted to a MySQL type"

Inserting multiple MySQL records using Python
Error: Python 'tuple' cannot be converted to a MySQL type
ERROR CODE:
Traceback (most recent call last):
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 181, in to_mysql
return getattr(self, "_{0}_to_mysql".format(type_name))(value)
AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in _process_params
res = [to_mysql(i) for i in res]
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in <listcomp>
res = [to_mysql(i) for i in res]
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 184, in to_mysql
"MySQL type".format(type_name))
TypeError: Python 'tuple' cannot be converted to a MySQL type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "python_mysql_2.py", line 22, in <module>
my_cursor.execute(mike_placeholders,records_list)
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 557, in execute
psub = _ParamSubstitutor(self._process_params(params))
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 437, in _process_params
"Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type
Python Code:
#import sql.connector
import mysql.connector
#Create connection, added db we created#
connection = mysql.connector.connect(
host='localhost',
user='root',
password='123',
database='testdb_1'
)
#Create cursor for the connection
my_cursor = connection.cursor()
#Create SQL statement with placeholders and put in variable
mike_placeholders="INSERT INTO users (name,email,age) VALUES (%s, %s, %s) "
#Create list (array) of records
records_list = [('Tim','Tim#tim.com',32), ('Mary','Mary#mary.com',40), ('Sam','Sam#sam.com',50), ('Fred','Fred#fred.com',22) ]
#Execute cursor, requires SQl statement variable, record variable
my_cursor.execute(mike_placeholders,records_list)
#Commit the connection to make the change on the database
connection.commit()
Ahhh, I used the wrong Python term.
I should have used executemany when working with a tuple.
my_cursor.executemany(mike_placeholders,records_list)
You can't pass a list to my_cursor.execute(), you need to iterate over the list:
for values in records_list:
my_cursor.execute(mike_placeholders, values)
Or you could repeat the (%s, %s, %s) multiple times and do it all in a single query by flattening the list of tuples.
mike_placeholders="INSERT INTO users (name,email,age) VALUES " + ", ".join(["(%s, %s, %s)"] * len(records_list))
my_cursor.execute(mike_placeholders, sum(records_list))
use my_cursor.executemany(mike_placeholders,records_list)
If you have multiple elements which are saved in a list or tuple then use,
cursor.executemany(query,list) or cursor.executemany(query,tuple)
You must use a for loop and INSERT item by item
for x in records_list:
my_cursor.execute(mike_placeholders, x)

sql compact connection error in python

connstr = """Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;DataSource=first.sdf;"""
conn = adodbapi.connect(connstr)
cur = conn.cursor()
getresult="select * from ft"
cur.execute(getresult)
result=cur.fetchall()
How can i solve the following error?
Traceback (most recent call last):
File "e:\python1\sqlcompactdb\compact.py", line 7, in <module>
connection = adodbapi.connect(connection_string)
File "C:\Users\khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect
raise api.OperationalError(e, message)
adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",), 'Error opening connection to "Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0; Data Source=E:\\python1\\sqlcompact\\first.sdf;"')
As the error implies, this issue stems from an error when the module tries to make an ADO database connection.
Specifically, when the following code executes
pythoncom.CoInitialize()
c = win32com.client.Dispatch('ADODB.Connection')
This is most likely due to hardware issues like the lack of the correct provider for the needed connection.
Solutions to a similar problem can be found at Connecting to SQLServer 2005 with adodbapi

Python SQL execution fails randomly with Traceback (most recent call last):

I am executing some sqls in loop . mostly the code works fine and completes the loop. But some time python fails with Traceback issue after 2-3 hours. When i checked the SQL execution in DB, it executed but python did not continue the next execution loop and got stuck. Throws the error after 2 hours
Executing SQL :
sql = f.read()
cursor.execute(sql)
Error :
Traceback (most recent call last):
...
...
File "/home/lib/python2.7/site-packages/pgdb.py", line 480, in __exit__
self.rollback()
File "/home/lib/python2.7/site-packages/pgdb.py", line 519, in rollback
raise _op_error("can't rollback")
pg.OperationalError: can't rollback
Here is the full code in loop:
dt = new_execution_timestamp_for(job, dependencies, cursor)
if dt is not None:
logging.info("Running job %s", job)
with open(os.path.join(os.environ['ROOT'],
"share", job + ".sql")) as f:
sql = f.read()
if weekly is None or weekly == date.today().weekday():
if loops is not None:
loop_items = fetch_list("loops", loops, cursor)
timestamp = fetch_job_timestamp(job, cursor)
for item in loop_items:
cursor.execute(sql, {loops: item,
'last_executed': timestamp})
else:
cursor.execute(sql)
cursor.execute(UPDATE_TIMESTAMP, (dt, job))
else:
logging.info("Skipping job %s", job)

Python variable in SQL statement?

I am trying to pass a variable to an SQL statement which I will eventually use in an iterator in order to process a list of key values and store in a CSV, however I am having trouble getting the variable into the statement?
Here is my code:
import MySQLdb as mdb
from MySQLdb import cursors
import csv
con = mdb.connect('172.16.7.50', 'root', 'abcd2014', 'templog')
tablename = 'pitemp'
with con:
cursor = con.cursor()
cursor.execute("SELECT temp, time FROM %s", (tablename,))
fid = open('new.csv','w')
writer = csv.writer(fid, delimiter=',')
writer.writerow([ i[0] for i in cursor.description ]) # heading row
writer.writerows(cursor.fetchall())
print 'finished!'
I have tried a selection of different bracket combinations as found on stack overflow but they all result in the following error:
Traceback (most recent call last):
File "/home/tom/PycharmProjects/TomsSQL2CSV/sql2csv.py", line 11, in <module>
cursor.execute("SELECT temp, time FROM %s", (vari,))
File "/home/tom/.local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/tom/.local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''pitemp'' at line 1")
You should be using '?' for parameter bindings in your sql string not
python format specifiers (you are after all writing sql here not
python).
cursor.execute("SELECT temp, time FROM ?", (tablename,))

MySQLdb execute

I have searched high and low on this site and many others and have found similar questions, but none of the answers have worked for me (usually just accounting for the tuple). I'm writing a python script to parse html pages and populate a database. I have almost everything working except the populating part...
Here is the code segment that deals with the mySQL database (note: using MySQLdb module in python)
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES (%s)", (test,))
conn.commit()
I was first testing it with parsed string but that wasn't working. This gives me 2 errors:
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 171, in execute
r = self._query(query)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 330, in _query
rowcount = self._do_query(q)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 294, in _do_query
db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax
; check the manual that corresponds to your MySQL server version for the right s
yntax to use near '%s)' at line 1")
Traceback (most recent call last):
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 171, in execute
r = self._query(query)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 330, in _query
rowcount = self._do_query(q)
File "C:\Python32\lib\site-packages\MySQLdb\cursors.py", line 294, in _do_query
db.query(q)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax
; check the manual that corresponds to your MySQL server version for the right s
yntax to use near '%s)' at line 1")
Why does this query not work!?
Update: After nearly pulling my hair out I decided to revert back to 2.7.3 and guess what... everything works now :D Should have know it was an error of that type... Thanks for the help none the less everyone!
I just ran across this too. After a lot of digging around, this was found to work (note the change to values):
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES ({0})", (test,))
conn.commit()
Background info: In the port of MySQLdb to Python 3 posted on an unofficial Python packages site, the execute function in cursors.py was modified to use format() rather than the % operator. Hence why %s remains in the SQL statement rather than being substituted. It looks like these changes were never upstreamed to the official source.
I think you don't need any tricky or advanced SQL; you just need to store and retrieve some stuff. So, I think an ORM might make life a lot easier for you.
I suggest you try using the Autumn ORM:
http://pypi.python.org/pypi/autumn/0.5.1
That is a small and simple ORM, easy to understand and work with.
Another good and popular ORM for Python is SQLAlchemy:
http://www.sqlalchemy.org/
You should write your code like this :
conn = MySQLdb.connect(user="root", passwd="xxxxx",db="nutrients")
cur = conn.cursor()
test = "Canned Corn"
cur.execute("INSERT INTO food (name) VALUES (%s)" % (test,) )
conn.commit()
If you are new to python and have some programming experience , then follow Dive Into Python book. Its free.

Categories

Resources