psycopg2 python 3 tutorial for beginners

psycopg2 python 3 tutorial for beginners: The PostgresSQL is the relational database used to create, read, and update a delete the web application data. The database driver will serve as the client for access to the postgresSQL server. It is designed for multithreading applications and creates the cursor. The psycopg 2 is implemented in c as libpq which results in both a secure and efficient way.
Here we can install psycopg by using pip to download from PyPI,
$pip install psycopg2
Or we can use setup.py as a source package,
$python setup.py build
$sudo python setup.py install
We can get the stand-alone package by installing the psycopg2 from PyPI,
$pip install psycog2-binary
The package is used for development and testing also to use the package built from sources.
It can be integrated with Python by using psycopg2 module.
$yum install python-psycope2
Then we use psycopg2 module as we create the connection of the object that represents the database.

Python psycopg2 module APIs:-

S. No.

API & Description

2

connection.cursor ():-it routine creates a cursor that will be used throughout your database programming using Python.

3

cursor. execute(sql [, optional parameters])
This routine executes an SQL statement which may be parameterized.

4

cursor.executemany(sql, seq_of_parameters):-
The routine will execute an SQL command against all parameter sequences found in the sequence sql.

5

cursor.callproc(procname[, parameters])
This routine will execute a stored database procedure with the given name.
Here the sequence of parameters must contain one entry for each argument.

6

cursor.rowcount
This read-only attribute will return the total number of database rows that have been modified, inserted, or deleted by the last execute*().

7

connection. Commit()
The  method will commit the current transaction.
The last call to commit () is not visible from the other database connections.

8

connection.rollback()
The method will roll back any changes to the database since the last call to commit ().

9

connection.close()
It will close the database connection.

10

cursor.fetchone()
This method will fetch the next row of a query and return a single sequence.
 

11

cursor.fetchmany([size=cursor.arraysize])
This routine fetches the next set of rows of a query and results returning a list.

12

cursor.fetchall()
This routine fetches all rows of a query result returning a list. The empty list is returned if no rows are available.

Connecting to Database:-


The code will show how to connect to an existing database.
If the database does not exist then it will be created and object will be returned.
Import psycopg2
Conn=psyvopg2.connect (database=”tstdb”, user=”pstgr”, password=”pass123”, host=”127.0.0.1”, port=”5432”)
Print “opened database successfully”
Psycopg is released under GNU Lesser General Public License which allows the use of both free and proprietary software.
Psycopg features: -

    • It supports Python versions from 2.5 to 3.4.
    • Supports PostgreSQL versions from 7.4 to 9.4.
    • The compliant implementation of the Python DB API specification for the database adapters.
    • Threads can be used in different connections or sharing the same connection.
    • Asynchronous no blocking I/O will integrate with co routine-based libraries.
    • They will COPY support.
    • Also have large objects support.
    • We can send and receive an asynchronous notification.
    • Two-phase commit is supported.
Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India