Not able to insert INT data in Python using MySQL connector - python

I'm using MySQL connector in Python and trying to insert an integer data, but I keep getting this error:
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s)' at line 1
My code looks like this:
medDosage = int(''.join(filter(str.isdigit, '42mg')))
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", medDosage)
db.commit()
And this statement has been working just fine for all other variables, and somehow for INT value, it does not work. I tried inserting the string variable instead of int, but doesn't work. Also tried to convert the value such as int(medDosage) to make sure it's the right type, but still doesn't work. I know my syntax is correct so I cannot really understand the error. Can you please help why this error is showing?
Thank you in advance.

You need to ensure the last argument is a tuple:
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", (medDosage,))

Related

How to use use bind variables in mysql and python

I'm using mysql with python, I got an error when using a bind varialbe. Actually I don't know how to use a bind variable. Please help me with my problem.
Here is my code:
iouNo=textiv1.get()
cursor.execute("UPDATE iou_table SET IOU_status=4 WHERE IOU_No= :iouNo",{'iouNo':iouNo})
mydb.commit()
This is the error I got
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 ':'iouNo'' at line 1
I want to use the iouNo variable inside the SQL query... How can I do it?
You can read up on that iin the manual from MySQL.
%s is used as place holde.
If you only have one parameter or variable you still have tpo use a 2 dimensional tuple,
iouNo=textiv1.get()
cursor.execute("UPDATE iou_table SET IOU_status=4 WHERE IOU_No= %s;",(iouNo,))
mydb.commit()

insertion went wrong 1064 (42000): You have an error in your SQL syntax;

Insertion went wrong 1064 (42000): 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 '%d,%d,%d)' at line 1
sql =""" INSERT INTO carprice(carmodel, mileage,sell_price,age) VALUES (%s,%d,%d,%d)"""
for x in list(csvdata('carprice.csv')):
cur.execute(sql,(x[0],x[1],x[2],x[3]))
con.commit()
You are not passing the arguments,
cur.execute(sql,(x[0],x[1],x[2],x[3]))
You need to use % sign in order to pass arguments.
cur.execute(sql%(x[0],x[1],x[2],x[3]))
EDIT:
To fix this error posted in the comment section. You need to check whether you are passing integer value. If you need to convert string value to integer, you can use the below code.
cur.execute(sql%(x[0],int(x[1]),int(x[2]),int(x[3])))

Syntax error when inserting strings into MySQL using PyMySQL

I frequently use pymysql to insert data into a MySQL server.
When inserting strings, I usually (but not every time) receive: pymysql.err.ProgrammingError: (1064, ...) when I insert a string using the code (where refers to a varchar):
cursor.execute("Insert into table (column) values (%s)", (stringVar))
Typically I have to do something like:
cursor.execute("Insert into table (column) values ('"+stringVar+"')"))
However, sometimes that throws the same error and I have to do something like:
stringVar="'"+stringVar
stringVar=stringVar+"'"
cursor.execute("Insert into table (column) values ("+stringVar+")")
This just isn't a feasible way to program this operation.
I assume I am messing up something simple but I cannot figure out what this is. I use pymysql a lot and this error is really starting to wear on me. Any help would be much appreciated!
cursor.execute('INSERT INTO table (column) VALUES (?)', (stringVar,))
Whenever you're trying to directly format a string into a query like that, it's basically always a sign you're doing something wrong. Every python database interface I'm aware of has a way to pass parameters to queries like above. Note that having the stringVar contained within an iterable is required.

python- mysql error when I use %s in execute

I have to delete some dates from mysql by python.
I have tables over 2000. so, I need to finish this code... I can't handle this much by clicking my mouse. I really need help.
well, my guess was like this
sql ="delete from finance.%s where date='2000-01-10'"
def Del():
for i in range(0,len(data_s)):
curs.execute(sql,(data_s[i]))
conn.commit()
Howerver, it doesn't work.
I just though
when I just type like this , it works.
>>> query="delete from a000020 where date ='2000-01-25'"
>>> curs.execute(query) //curs=conn.cursor()
But if I add %s to the syntax, it doesn't work..
>>> table='a000050'
>>> query="delete from %s where date ='2000-01-25'"
>>> curs.execute(query,table)
ProgrammingError: (1064, u"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 ''a000050' where date ='2000-01-25'' at line 1")
it doesn't work too.
>>> curs.execute(query,(table))
ProgrammingError: (1064, u"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 ''a000050' where date ='2000-01-25'' at line 1")
a bit different... but same.
>>> curs.execute(query,(table,))
I have read many questions from here, but by just adding () or , it doesn't fixed...
Because I'm beginner for the python and mysql, I really need your help. Thank you for reading.
I had the same issue and I fixed by appending as:
def Del():
for i in range(0,len(data_s)):
x = "delete from finance." + data_s[i] + "where date='2000-01-10'"
print x # to check the sql statement :)
curs.execute(x)
conn.commit()
Good question,have a look at MySQLdb User's Guide
paramstyle
String constant stating the type of parameter marker formatting
expected by the interface. Set to 'format' = ANSI C printf format
codes, e.g. '...WHERE name=%s'. If a mapping object is used for
conn.execute(), then the interface actually uses 'pyformat' = Python
extended format codes, e.g. '...WHERE name=%(name)s'. However, the API
does not presently allow the specification of more than one style in
paramstyle.
Note that any literal percent signs in the query string passed to execute() must be escaped, i.e. %%.
Parameter placeholders can only be used to insert column values. They
can not be used for other parts of SQL, such as table names,
statements, etc.
Hope this helps.

Python MySQL insert NULL (None) value error

I am trying to insert a NULL value into a MySQL db int field with a python script. NOT NULL is off on the field so its not that. I have manually inserted a NULL value into database and that worked fine and my code works fine if I put a literal value in the place of None.
I have looked a several people's examples of how to do this and as far as I can tell there is nothing wrong in syntax.
Code:
length2 = None
cursor.execute("INSERT INTO tableName(Length) VALUES(%s)", length2)
Error:
Error 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
'%s)' at line 1
Any ideas?
The second argument to execute() should be a tuple (or list).
Please change your code to:
cursor.execute("INSERT INTO tableName (Length) VALUES (%s);", (length2,))

Categories

Resources