Python Forum
Python and Postgresql syntax select statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python and Postgresql syntax select statement
#1
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()


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


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Up Select Python comment (#) [solved] SpongeB0B 7 3,483 Sep-28-2025, 06:39 PM
Last Post: tester_V
  Python com PostgreSQL Anderson_Nup 0 520 Aug-01-2025, 06:38 PM
Last Post: Anderson_Nup
  Is there a Python solution to select unique frames from a video? rownong 1 1,399 Feb-02-2025, 09:56 AM
Last Post: Larz60+
  How to Connect to PostgreSQL Through Jump Server and SSH Tunnel using Python? nishans 1 6,179 Jan-02-2024, 10:37 AM
Last Post: khanzain
  Error using mariadb select query with form in python? shams 2 3,478 Jul-29-2021, 12:30 PM
Last Post: shams
  maintain a postgresql database using osm2pgsql apollo 1 3,615 Aug-03-2020, 10:33 PM
Last Post: Larz60+
  PostgreSQL psycopg2.errors.DuplicateColumn: column specified more than once rajnish_nationfirst 2 6,280 Jun-21-2020, 08:17 AM
Last Post: ibreeden
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 4,587 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  syntax error on return statement l_butler 5 5,141 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  Syntax Error (elif statement) Kanashi 0 5,192 Nov-20-2019, 11:29 PM
Last Post: Kanashi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020