Python Forum

Full Version: Python and Postgresql syntax select statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have got a problem with the bold line with attached error :

File "list.py", line 16, in <module>
flight= cur.execute("select origin, destination, duration from flights where id = :flight_id", {"flight_id": flight_id}).fetchone()
psycopg2.errors.SyntaxError: syntax error at or near ":"
LINE 1: ...in, destination, duration from flights where id = :flight_id


Can you help me guys, what is the problem?

/////////////////////////////////////////////////

import psycopg2
# create a connection to the databse
conn= psycopg2.connect( database="mydb", user="postgres", password="system123456", host="localhost", port="5432")
print("Connected to database")

# creating cursor to the database to interect with the data

cur= conn.cursor()

flight_id = int(input("\nFlight ID: "))

flight= cur.execute("select origin, destination, duration from flights where id = :flight_id", {"flight_id": flight_id}).fetchone()

flight_id = int(input("\nFlight ID: "))
flight= cur.execute("select origin, destination, duration from flights where id = :flight_id", {"flight_id": flight_id}).fetchone()

if flight is None:
print("Error: No such flight.")
else:
passengers=cur.execute("select name from passengers where flight_id=:flight_id",{"flight_id": flight_id}).fetchall()
print("The passengers ")
for passenger in passengers:
print(passenger.name)
if len(passenger)==0:
print("No passengers.")

#close database
cur.close()
conn.close()


////////////////////////////////////////
The way you're trying to pass parameters is wrong. See the documentation for how to do it correctly.