Quantcast
Channel: MySQL Forums - Connector/Python
Viewing all articles
Browse latest Browse all 384

How do I use a variable from a for loop in my sql query (no replies)

$
0
0
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.

Viewing all articles
Browse latest Browse all 384

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>