How do I use a variable from a for loop in my sql query
Hello there,
I am making a python script that connects to my mysql database and selects the name corresponding to the first id from the table.
It will then do an image search and download an image to which I can later add a path in my database.
This loops until it has done every id so every row has it's own unique image.
The script works already, however, I am having trouble with taking the id from python and putting it in the sql query.
Is there a way I can sort of close out of the query, enter the id variable, and resume the sql query with mysql.connector?
This is what it looks like right now:
for i in range(1, 2642):
habe = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="habe"
)
id = i
mycursor = habe.cursor()
mycursor.execute("SELECT name FROM processor WHERE id = %s")
myresult = mycursor.fetchone()
i += 1
As I was looking this up, I saw people were recommending using %s as a placeholder but this doesn't work for me.
When I do this, it tells me I have an error in my sql syntax.
Subject
Written By
Posted
How do I use a variable from a for loop in my sql query
January 26, 2020 10:22AM
Sorry, you can't reply to this topic. It has been closed.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion
of Oracle or any other party.