I am running a query where it is showing error that I need to store in a variable and used that message in if-else condition. Please help me to solve this?
Example:
query = "ALTER DATASHARE table1 ADD TABLE table2;"
cur.execute(query)
error:
Relation table2 is already added to the datashare table1
I want to store above error in variable and use in if-else condition.
I am trying to insert/append into access a dataframe using pyodbc. However; when I run the code, I get an error: ProgrammingError: ('The SQL contains 21 parameter markers, but 1 parameter were supplied', 'HY000')
my sample code is: for row in tDjango: cursor.execute( 'INSERT INTO TDjango (Eid, Eventtype, Location, Lat, Lon, Created, TMCClosed,FirstArrival(min), PatrolArrival(min), TowArrival(min), LanesCleared(min), RoadwayCleared(min),Camera, DayofWeekOpened, DayofWeekClosed, sameDay, confirmClosed, confirmFirstAr, confirmPtrl, confirmTow, confirmLnClear) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)',tDjango) conn.commit()
I’m not entirely sure what I am missing in the SQL statement to make the error go away.
Instead of using cursor.execute(), I used cursor.executemany().
I'm resorting to overflow as a last ditch effort. I have been facing this bug for an incredibly long time and I have had no luck. I will try anything.
My sql command is
update_query = """
UPDATE users_data
SET stocks = """+str(stock)+"""
WHERE id = """+str(userid)
cursor = connection.cursor(buffered=True)
cursor.execute(update_query)
connection.commit()
context:
the variable stock is a list before I use str() on it.
userid is an int before I use str() on it.
the column stocks has a datatype of mediumtext
the column id has a datatype of text
the error I receive 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 '['gme', '1']
WHERE id = 407081272293326858' at line 2
Please help
I am trying to insert values into a row of mysql database in a way that not be vulnerable to injection, but gives my syntax error. This is a piece of my code which causes the error:
cursor.execute("INSERT INTO api.mytable(id) VALUES (:id);", {"id": 1})
and error:
ERROR in connection: (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 ':id)' at line 1")
code you please tell me what's the wrong with my code?
I am assuming id is given as some kind if input! Hence you can always check for the required format and allow only required ones! This is to avoid SQL injection!. Hence the natural formatting as shown below should do the job! And this is very basic level checking!
id_in = input("Here is the id taken " ) ## can be through any source . It is just an example
if isinstance(id_in,int): ##Please mention the required format here I am assuming it as integer
cursor.execute("INSERT INTO api.mytable(id) VALUES (%s);", (id_in))
else:
##do some stuff here
I believe I've exhausted all options (given by current experience with MySQL which is not much) for a syntax error I get when trying to create a MySQL table in Python 3.7. Some background; I'm trying to create a table in each each loop iteration as follows:
index=0
for rawdat in get_WeatherData():
mycursor.execute("CREATE TABLE "+datetime.datetime.today().strftime('%Y-%m-
%d')+BoMList[0][index]+" (Date_Time VARCHAR(255), Wind_Speed FLOAT(12,9),
Rain_Trace FLOAT(12,9))")
index+=1
The list BoMList[0][index] that is being called is a string value (weather station name if your interested) and I'm wanting the date in the table title as well.
The error I get is:
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 '2018-12-05S_Observatory_Hills (Date_Time VARCHAR(255),
Wind_Speed FLOAT(12,9), R' at line 1
Is it possible I'm using the wrong format for the MySQL syntax string:
"CREATE TABLE "+datetime.datetime.today().strftime('%Y-%m-%d')+BoMList[0][index]+" (Date_Time VARCHAR(255), Wind_Speed FLOAT(12,9),Rain_Trace FLOAT(12,9))"?
I'm going off the syntax provided in: https://www.w3schools.com/python/python_mysql_create_table.asp
Lastly rawdat and get_WeatherData() are not used until later.
Interested to hear what you guys thing, cheers.