How to use use bind variables in mysql and python - 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()

Related

Not able to insert INT data in Python using MySQL connector

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,))

inserting data in a mysql database via python

I'm trying to insert variables as data into a database
I'm using this (part of it)
query = "INSERT INTO table_name (name) VALUES (%S)"
aa="naam"
cursor.execute(query,aa)
and everytime, I get the following error message:
"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"
no matter what I try to do, I'm getting this message (put it in """, put () around it, ...)
Hope someone can help me
The format needs to be lowercase, so change the query into: query = "INSERT INTO table_name (name) VALUES (%s)".

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])))

update data base with mariaDB python

I am trying to update a data base with mariaDb command. I have used Mysql before with no problems and have checked my script for the mariDb script but when I run it python is stating the following
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s WHERE id = 2' at line 1
my script is below can someone please see what I am doing wrong
cursor = mariadb_connection.cursor()
try:
cursor.execute ("""UPDATE heating SET garage=%s WHERE id = 2""", (garageTemp))
except () as e:
print (e)
This is within a function and the garageTemp is a variable taken from part of the full program
Typical got this working just after posting the question. Did not realise I could not put the id = 2 within the statement.
So the following works
("""UPDATE heating SET garage=%s WHERE id=%s""", (garageTemp, 2))

SQL syntax error on INSERT

I keep running into an error from the following statement:
cursor.execute("""INSERT into financial_statements (url)
VALUES (%s) WHERE provider=%s AND date=%s""", (url, provider, date))
The error I get is:
_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
'WHERE provider='ANGEL' AND date='2012-03-01'' at line 2")
You can't have a WHERE clause for an INSERT statement.
Maybe you meant to perform an UPDATE?
UPDATE financial_statements
SET url = %s
WHERE provider=%s AND date=%s
It makes no sense to use WHERE in an INSERT statement - there is nothing to restrict.
If you want to modify an existing row, use UPDATE:
UPDATE financial_statements SET url=%s WHERE provider=%s and DATE=%s

Categories

Resources