0

I'm trying to select some data from my table using Python, but each time I have a new value to select using a list. Here is my table.

CREATE TABLE IF NOT EXISTS stops (
 stop_id varchar(3) PRIMARY KEY,
 stop_code varchar(10),
 stop_name varchar(30),
 stop_desc varchar(10),
 stop_lat varchar(20),
 stop_lon varchar(20),
 zone_id varchar(20),
 stop_url varchar(20),
 location_type varchar(20),
 parent_station varchar(20)
)

I tried to select data using this code.

mycursor.execute("SELECT stop_id FROM stops where stop_name=station")
stopid = mycursor.fetchone()

Where station is a variable that i've been using all long the code.

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
  • Hello, it's difficult to understand your question. Also, please format your question with the tick character so it's more legible – Zac Jul 22 '19 at 16:20

1 Answers1

0

you'd need to pass that variable as a parameter along with the query to the database, e.g:

mycursor.execute(
    "SELECT stop_id FROM stops WHERE stop_name=%s",
    [station],
)
stopid = mycursor.fetchone()

see https://stackoverflow.com/a/775399/1358308

Sam Mason
  • 15,216
  • 1
  • 41
  • 60