I keep getting the error information after running this line of code
query = ("INSERT INTO Movies" "(movie_id,movie_name,movie_year,duration,score,storyline,genre,poster)"
"VALUES (%(ID)s,%(name)s,%(year)s,%(runtime)s,%(rating)s,%(storyline)s,%(genre)s,%(links)s)") #args = (ID,name,year,runtime,rating,storyline,genre,links)
cursor.execute(query)
con.commit()
The format is from mysql document
the error message is
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 '%(ID)s,%(name)s,%(year)s,%(runtime)s,%(rating)s,%(storyline)s,%(genre)s,%(links)' at line 1
You are missing the query parameters:
cursor.execute(query, args)
where args, since you are using named parameters, has to be a dictionary where the keys should correspond to the parameter names:
args = {
"name": "some name",
"year": 1962,
# ...
}
As a side note, you can put your query into a multi-line string for readability:
query = """
INSERT INTO
Movies
(movie_id, movie_name, movie_year, duration, score, storyline, genre, poster)
VALUES
(%(ID)s, %(name)s, %(year)s, %(runtime)s, %(rating)s, %(storyline)s, %(genre)s, %(links)s)
"""
Related
I am updating an existing MySQL table with pymysql but failed. The error is:
pymysql.err.ProgrammingError: (1064, "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 'ON DUPLICATE KEY
And my code is here:
with conn.cursor() as cursor:
sql = '''
INSERT INTO `Tag` (`title`,`weight`) VALUES (%s,%s)
ON DUPLICATE KEY
UPDATE
title = VALUES(title),
weight = VALUES(weight)
'''
L1 = ['ins', 'ig', 'twitter', 'tiktok', 'ig']
values = list(set([(item, L1.count(item)) for item in L1]))
cursor.executemany(sql, values)
conn.commit()
I want to receive json data from MQTT and store it in my database.
When am executing my code am receiving this error:
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)' at line 1
There is my code :
import mysql.connector
import json
mydb = mysql.connector.connect(
host="localhost",
user="***",
passwd="***",
database="database"
)
mycursor = mydb.cursor()
def DATA_REPARTITION(Topic, jsonData):
if Topic == "test":
#print ("Start")
INSERT_DEBIT(jsonData)
def INSERT_DEBIT(jsonData):
#Read json from MQTT
print("Start read data to insert")
json_Dict = json.loads(jsonData)
debit = json_Dict['debit']
#Insert into DB Table
sql = ("INSERT INTO debit (data_debit) VALUES (%s)")
val=(debit)
mycursor.execute(sql,val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
mycursor.close()
mydb.close()
Thanks for your help, am working on this problem for the last 2 days.
You've written your parameterized query properly for MySQL:
sql = ("INSERT INTO debit (data_debit) VALUES (%s)")
The problem is that you're passing in the arguments wrong:
val=(debit)
mycursor.execute(sql,val)
The parentheses don't make debit into a tuple of 1 value. They don't do anything at all; val is just the same value as debit.
But execute wants a sequence of separate values, not 1 value.
To fix this, you need to add a comma. Commas are what create tuples in Python:
val = debit,
If you're wondering why this raises a SQL error, instead of a Python error about val not being iterable… Most likely, val is a string. And strings are iterable. They just iterate their characters. If val is, say, '$100', then you're passing the arguments '$', '1', '0', and '0', to fit a parameterized query with only one parameter.
I am trying to get the ID of a newly inserted row by using OUTPUT. However, I encountered the HY010 error. The following query/code is what I use:
string = """
SET NOCOUNT ON;
DECLARE #NEWID TABLE(ID INT);
INSERT INTO dbo.t1 (Username, Age)
OUTPUT inserted.id INTO #NEWID(ID)
VALUES(?, ?)
SELECT ID FROM #NEWID
"""
cursor.execute(string, "John Doe", 35)
cursor.commit()
id = cursor.fetchone()[0]
the last line id = cursor.fetchone()[0] led to a HY010 error (see below). Any advice would be greatly appreciated!
pyodbc.Error: ('HY010', '[HY010] [Microsoft][ODBC SQL Server Driver]Function sequence error (0) (SQLFetch)')
I was able to reproduce your issue, and I was able to avoid it by retrieving the id value immediately after the INSERT and before the commit. That is, instead of
cursor.execute(string, "John Doe", 35)
cursor.commit()
id = cursor.fetchone()[0]
I did
cursor.execute(string, "John Doe", 35)
id = cursor.fetchone()[0] # although cursor.fetchval() would be preferred
cursor.commit()
For me only this worked with Azure SQL Serverless (using pyodbc==4.0.28):
cursor.execute(insert_statement, param_value_list)
cursor.execute("SELECT ##IDENTITY AS ID;")
return cursor.fetchone()[0]
If you're using SQLAlchemy with an engine, then you can retrieve the PyODBC cursor like this before running the query and fetching the table ID.
connection = sql_alchemy_engine.raw_connection()
cursor = connection.cursor()
result = cursor.execute(
"""
INSERT INTO MySchema.MyTable (Col1, Col2) OUTPUT INSERTED.MyTableId
VALUES (?, ?);
""",
col1_value,
col2_value,
)
myTableId = cursor.fetchone()[0]
cursor.commit()
print("my ID is:", myTableId)
I'm using Connector/Python to update a MySQL database from a Python script.
update_table = ("UPDATE Users "
"SET `%s` = %s "
"WHERE Id = %s ")
cursor.execute(update_table, (columnname, value, id))
And I get this error:
ProgrammingError: 1054 (42S22): Unknown column ''ColumnName'' in 'field list'
while I do have a column called ColumnName. I think there's some problem with the quotes, i.e. it might be looking for 'ColumnName' instead of ColumnName, but, for example, if I remove the backticks (`) and the update_table looks like this:
update_table = ("UPDATE Users "
"SET %s = %s "
"WHERE Id = %s ")
cursor.execute(update_table, (columnname, value, id))
I get this other error:
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 ''ColumnName' = 'Somebody' WHERE Id = '0000'' at line 1
Any idea about how to fix this?
Thanks in advance.
To the ones wondering about how I solved this problem:
update_table = ("""UPDATE Users
SET %s = '%s'
WHERE Id = '%s' """)
cursor.execute(update_table%(columnname, value, id))
Thanks to furas and John Ruddell for the tips.
Look at the code in python shell:
>>> s = u'赵孟頫'.encode('gbk')
>>> s
'\xd5\xd4\xc3\xcf\xee\\'
The last byte of '赵孟頫' is \x5c, the same as backslash. And it causes a sql 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 ''?????\\')' at line 4
Here is my code:
# db is mysql.connector object
sql = '''
INSERT INTO scraped_products(
site_prd_id,site_id,brand)
VALUES(
%(site_prd_id)s,%(site_id)s,%(brand)s)
'''
dat = {
'site_prd_id' : 'test',
'site_id' : 1,
'brand' : u'赵孟頫'.encode('gbk'),
}
self.db.ping(True, 3, 1)
self.db.cursor().execute(sql, dat)
I have a solution which would need some extra work to get it working. The following code example is converting the data into a MySQL Hexadecimal Literal and sends it to MySQL without escaping, quoting or converting it. It's a bit a different way of executing queries, but I hope it will serve for now:
import mysql.connector
cnx = mysql.connector.connect(database='test', user='root',
charset='gbk', use_unicode=False)
cur = cnx.cursor()
cur.execute("DROP TABLE IF EXISTS gbktest")
table = (
"CREATE TABLE gbktest ("
"id INT AUTO_INCREMENT KEY, "
"c1 VARCHAR(40)"
") CHARACTER SET 'gbk'"
)
cur.execute(table)
def gbk_to_hexstr(value):
"""Convert value to Hexadecimal Literal for MySQL
"""
return "0x{0}".format(''.join(
["{0:x}".format(ord(c)) for c in value.encode('gbk')]))
# Convert your Unicode data using gbk_to_hexstr
data = {
'c1' : gbk_to_hexstr(u'赵孟頫'),
}
# Use MySQLCursor.execute() _not_ passing data as second argument
cur.execute("INSERT INTO gbktest (c1) VALUES ({c1})".format(**data))
cur.execute("SELECT c1 FROM gbktest")
# Print the inserted data as Unicode
for row in cur:
print(row[0].decode('gbk').encode('utf8'))