Call PostgreSQL Databases with Python | Python Bits | Kovolff

Veröffentlicht am: 20 November 2020
auf dem Kanal: kovolff
144
3

Step 1
Import psycopg2

If you do not have this module, just install it with pip
i.e. pip install psycopg2

Step 2
Get Database details: Server Path, Server Port, Database Name, Username, Password
i.e.
db_path = ‘127.0.0.1’
db_port = ‘5433’
db_name = ‘exp’
db_user = ‘py_user’
db_password = ‘py_user’

Make sure the username has access / privileges to the database table or view you need to query

Step 3
Build connection
i.e.
database_connection = psycopg2.connect(user = db_user, password = db_password, host = db_path, port = db_port, database = db_name)

database_cursor = database_connection.cursor()

Step 4
Execute SQL
i.e.
databse_cursor.execute(‘SELECT * FROM fundamental_infos_clean;’)
Found_data = database_cursor.fetchall()

To get the column names from PostgreSQL
Use the cursor property description
i.e. database_cursor.description
This yields a tuple of tuples. Each of the individual tuples contains two items: field name and the type code

So to get the field name, you need to loop through the outer tuple, and then extract the first element of each of the individual tuples.

i.e.
Column_name = []
For i in database_cursor.description:
column_names.append(i[0])

#python #postgres #sql #postgresql #databases


Auf dieser Seite können Sie das Online-Video Call PostgreSQL Databases with Python | Python Bits | Kovolff mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer kovolff 20 November 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 144 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!