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
  How to Connect to PostgreSQL Through Jump Server and SSH Tunnel using Python? nishans 1 847 Jan-02-2024, 10:37 AM
Last Post: khanzain
  Error using mariadb select query with form in python? shams 2 1,956 Jul-29-2021, 12:30 PM
Last Post: shams
  maintain a postgresql database using osm2pgsql apollo 1 2,255 Aug-03-2020, 10:33 PM
Last Post: Larz60+
  PostgreSQL psycopg2.errors.DuplicateColumn: column specified more than once rajnish_nationfirst 2 3,694 Jun-21-2020, 08:17 AM
Last Post: ibreeden
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 2,879 Jun-10-2020, 06:16 PM
Last Post: ibreeden
  syntax error on return statement l_butler 5 3,027 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  Syntax Error (elif statement) Kanashi 0 3,638 Nov-20-2019, 11:29 PM
Last Post: Kanashi
  Create table with psycopg2 on postgreSQL DB yhecohen 2 3,276 Aug-23-2019, 05:56 AM
Last Post: massimo_m
  Error while fetching data from PostgreSQL linu 3 4,233 May-13-2019, 02:38 PM
Last Post: rxndy
  SELECT statement query question using a variable DT2000 2 2,983 Feb-23-2019, 07:35 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

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