$ version.py PostgreSQL 11.1, compiled by Visual C build 1914, 64-bit This is the output. In the second example, we again get the version of the PostgreSQL database. This time we use the with keyword. Py-postgresql is a project dedicated to improving the Python client interfaces to PostgreSQL. At its core, py-postgresql provides a PG-API, postgresql.api, and DB-API 2.0 interface for using a PostgreSQL database. Py-postgresql is a project dedicated to improving the Python client interfaces to PostgreSQL. At its core, py-postgresql provides a PG-API, postgresql.api, and DB-API 2.0 interface for using a PostgreSQL database.
The PostgreSQL can be integrated with Python using psycopg2 module. sycopg2 is a PostgreSQL database adapter for the Python programming language. psycopg2 was written with the aim of being very small and fast, and stable as a rock. You do not need to install this module separately because it is shipped, by default, along with Python version 2.5.x onwards.
If you do not have it installed on your machine then you can use yum command to install it as follows −
To use psycopg2 module, you must first create a Connection object that represents the database and then optionally you can create cursor object which will help you in executing all the SQL statements.
The following are important psycopg2 module routines, which can suffice your requirement to work with PostgreSQL database from your Python program. If you are looking for a more sophisticated application, then you can look into Python psycopg2 module's official documentation.
S. No. | API & Description |
---|---|
1 | psycopg2.connect(database='testdb', user='postgres', password='cohondob', host='127.0.0.1', port='5432') This API opens a connection to the PostgreSQL database. If database is opened successfully, it returns a connection object. |
2 | connection.cursor() This routine creates a cursor which will be used throughout of your database programming with Python. |
3 | cursor.execute(sql [, optional parameters]) This routine executes an SQL statement. The SQL statement may be parameterized (i.e., placeholders instead of SQL literals). The psycopg2 module supports placeholder using %s sign For example:cursor.execute('insert into people values (%s, %s)', (who, age)) |
4 | cursor.executemany(sql, seq_of_parameters) This routine executes an SQL command against all parameter sequences or mappings found in the sequence sql. |
5 | cursor.callproc(procname[, parameters]) This routine executes a stored database procedure with the given name. The sequence of parameters must contain one entry for each argument that the procedure expects. |
6 | cursor.rowcount This read-only attribute which returns the total number of database rows that have been modified, inserted, or deleted by the last last execute*(). |
7 | connection.commit() This method commits the current transaction. If you do not call this method, anything you did since the last call to commit() is not visible from other database connections. |
8 | connection.rollback() This method rolls back any changes to the database since the last call to commit(). |
9 | connection.close() This method closes the database connection. Note that this does not automatically call commit(). If you just close your database connection without calling commit() first, your changes will be lost! |
10 | cursor.fetchone() This method fetches the next row of a query result set, returning a single sequence, or None when no more data is available. |
11 | cursor.fetchmany([size=cursor.arraysize]) This routine fetches the next set of rows of a query result, returning a list. An empty list is returned when no more rows are available. The method tries to fetch as many rows as indicated by the size parameter. |
12 | cursor.fetchall() This routine fetches all (remaining) rows of a query result, returning a list. An empty list is returned when no rows are available. |
The following Python code shows how to connect to an existing database. If the database does not exist, then it will be created and finally a database object will be returned.
Here, you can also supply database testdb as name and if database is successfully opened, then it will give the following message −
The following Python program will be used to create a table in previously created database −
When the above given program is executed, it will create COMPANY table in your test.db and it will display the following messages −
The following Python program shows how we can create records in our COMPANY table created in the above example −
When the above given program is executed, it will create given records in COMPANY table and will display the following two lines −
The following Python program shows how we can fetch and display records from our COMPANY table created in the above example −
When the above given program is executed, it will produce the following result −
The following Python code shows how we can use the UPDATE statement to update any record and then fetch and display updated records from our COMPANY table −
When the above given program is executed, it will produce the following result −
The following Python code shows how we can use the DELETE statement to delete any record and then fetch and display the remaining records from our COMPANY table −
When the above given program is executed, it will produce the following result −