psycopg2 query

import psycopg2

try:
   connection = psycopg2.connect(user="sysadmin",
                                  password="pynative@#29",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="postgres_db")
   cursor = connection.cursor()
   postgreSQL_select_Query = "select * from mobile"

   cursor.execute(postgreSQL_select_Query)
   print("Selecting rows from mobile table using cursor.fetchall")
   mobile_records = cursor.fetchall() 
   
   print("Print each row and it's columns values")
   for row in mobile_records:
       print("Id = ", row[0], )
       print("Model = ", row[1])
       print("Price  = ", row[2], "\n")

except (Exception, psycopg2.Error) as error :
    print ("Error while fetching data from PostgreSQL", error)

finally:
    #closing database connection.
    if(connection):
        cursor.close()
        connection.close()
        print("PostgreSQL connection is closed")

4.67
3

                                    #!/usr/bin/env python
# -*- coding: utf-8 -*-

import psycopg2
import sys

con = None

try:

    con = psycopg2.connect(database='testdb', user='postgres',
        password='s$cret')

    cur = con.cursor()
    cur.execute('SELECT version()')

    version = cur.fetchone()[0]
    print(version)

except psycopg2.DatabaseError as e:

    print(f'Error {e}')
    sys.exit(1)

finally:

    if con:
        con.close()

4.67 (3 Votes)
0
4.1
10

                                    >>> import psycopg2

# Connect to an existing database
>>> conn = psycopg2.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
>>> cur = conn.cursor()

# Execute a command: this creates a new table
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")

# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion (no more SQL injections!)
>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
...      (100, "abc'def"))

# Query the database and obtain data as Python objects
>>> cur.execute("SELECT * FROM test;")
>>> cur.fetchone()
(1, 100, "abc'def")

# Make the changes to the database persistent
>>> conn.commit()

# Close communication with the database
>>> cur.close()
>>> conn.close()

4.1 (10 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
python psycopg2 select example select value from table psycopg2 psycopg2 query with parameters psycopg2 select list psycopg2 execute query quesry psycopg2 psycopg2 query database psycopg select psycopg2 query table psycopg2 select example where psycopg2 select all python3 psycopg2 query psycopg2 select all from with psycopg2 %s select example psycopg2 select * from table where ? psycopg2 select with params psycopg2 select data from table postgresql with quote in select psycopg2 postgre select with IN psycopg2 psycopg2 query python python psycopg2 query documentation conn = psycopg2 """select * psycopg2 query select psycopg2 query parameters psycopg2 sql query psycopg2 get result of query psycopg2 postgre select psycopg select example copy_from psycopg2 example psycopg2 realdictrow psycopg2 select from table where psycopg2 view query select in psycopg2 how to make queries with psycopg2 query a database with psycopg2 queries in psycopg2 psycopg2 postgresql python print query psycopg2 arguments psycopg2 execute python postgresql fetcha psycopg2 boolean psycopg2 get data from table % after query psycopg2 psycopg2 select to list psycopg2 get all returned rows psycopg2 fetchall select postgres select query python psql query psycopg2 psycopg2 execute select into list python psycopg2 select psycopg2 tutorial pg-cursor example python python query postgresql database psycopg2 fetc run postges statement in python postgres psycopg2 example psycopg2 search psycopg2 where psycopg2 search select where psycopg 2 fetch psycopg2 get data into table psql query python psycopg2 do postgres fetchone read table psycopg2 psycopg2 select in list Postgresql psycopg2 SELECT data FROM table WHERE search and filter data from input Postgresql psycopg2 SELECT data FROM table WHERE Psycopg2 SELCT from data where psycopg2 get values from database python postgresql select Postgres sql SELECT*FROM WHERE psycopg2 Postgres sql SELECT*FROM Psycopg2 query postgresql with python python postgresql select example select records from postgres with python python3 + postgresql write sql query into a table pyscopg2 print database table psycopg2 return table select tables psycopg2 python psycopg2 example python postgresql query how to select all rows python postgres run sql query python postgresql psycopg2 fetch all table psycopg2 python example psycopg2 see tables psycopg2 cursor execute select query how to check value into db using psycopg2 psql python how to check value into db using psycopg2 psql python cursor.execute psql psycopg2 execute a query python select query psql pull data from database with psycopg2 psycopg2 query example select query not working psyopg2 psycopg2 select query psycopg2 query selecting rom sql table psycopg2 select statement not working psycopg2 select statement psycopg2 select all from table postgres python select query in postgresql python how to run a custom query in psycopg2 python postgres select python print records from postgres database psycopg2 usage postgres psycopg2 tutorial python get entire table from pg psycopg2 select cursor.fetchone postgresql connect Postgres Tutorial with psycopg2 how to select at databases in psycopy2 select id from variable table psycopg2 sql search like python psycopg2 psycopg2 select example psycopg2 example select retrieve value python psycopg2 select query in python with where clause postgres read from postgres python postgresql python cursor.execute psycopg2 raw sql python psycopg2 get query result psycopg2 example use content table postgresql python postgres python select where postgres python select
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source