SlideShare a Scribd company logo
A presentation on
psycopg2 python
module
by Dinesh Neupane [@neupanedinesh_] | Kathmandu
University
Psycopg2
A python driver for PostGreSQL
PostgreSQL
Python
Script
Background
 PostGreSQL database adapter for Python
Programming language
 Version 2 : original code completely rewritten for
better styling of classes for connection and cursor
 Postgres is type sensitive so we have to declare
types on each of our columns.
1
Background
● Robust engine that is implemented as a server
rather than a single file. As a server, Postgres
accepts connections from clients who can request a
SELECT, INSERT, or any other type of SQL query.
● This type of a design is called a client-server model,
where clients can interact with the server.
2
Requirements
 PostGreSQL 9.3 or higher
 Python 2.0 or higher
3
Installation
● Install python package installer (pip)
● For windows machine (to install package with all it’s
dependencies)
pip install psycopg2
4
Classes and Methods
● Connect()
Create a new database connection and returns a new
connection instance
connect("dbname=‘test_database’user=‘postgres'host='localhost'
password=‘italy'")
5
Connection Parameters
The basic connection parameters are:
● dbname – the database name (database is a
deprecated alias) user – user name used to
authenticate
● password – password used to authenticate
● host – database host address
● port – connection port number
6
Connection Example
Example:
import psycopg2
try:
conn = psycopg2.connect("dbname='test_database'
user='postgres' host='localhost' password='italy’”)
print "Connection Sucessful !! "
except:
print "unable to connect to the database"
7
Cursor()
Cursors are created by the connection.cursor()
method: they are bound to the connection for the
entire lifetime and all the commands are executed in
the context of the database session wrapped by the
connection.
8
Cursor()
The class cursor allows interaction with the database:
● Send commands to the database using using
methods like execute() and executemany()
● Retrieve data from the database by iteration or
using methods such as fetchone(), fetchmany() or
fetchall()
● cursor.description to get the metadata
9
Cursor()
● mogrify() - Return a query string after arguments
binding. The string returned is exactly the one that
would be sent to the database running the execute()
method or similar. Example:
>>> cur.mogrify("INSERT INTO table_one VALUES(6, 'Sarad
Chaudhary', 'Banke Nepalgunj’)”)
"INSERT INTO table_one VALUES(6, 'Sarad Chaudhary', 'Banke
Nepalgunj’)”
10
Create and Drop table
CREATE TABLE student (
student_id INTEGER UNIQUE,
student_name VARCHAR(50),
phone CHAR(10),
birth_date DATE,
);
This creates a new table costumers with different
attribute fields.
11
Adaptation of Python Values to
SQL Types
Python PostgreSQL
None Null
Bool Bool
Float Real, Double
Int, Long Smallint, integer, bigint
Decimal Numeric
Str Varchar
List Array
12
Closing a database session
Make the changes to the database persistent
● Conn.commit()
Close the communication with database
● Cur.close()
● Conn.close()
13
Create and Drop table
The student table is created in the database that you
are connected to. [first schema in search path]
If you want the table to be created in some other
schema, you can prefix the table name with the
schema qualifier, for example:
CREATE TABLE another_schema.student( ... );
14
Create and Drop table
Dropping a table is much easier than creating a table.
The syntax for the DROP TABLE command is:
DROP TABLE student;
The rentals table existed in some schema other than your current
schema, you would qualify the table name:
DROP TABLE other_schema.student;
15
SQL Transactions
● If you check the postgres
database now you would
notice that there actually
isn't a users table inside it.
● This isn't a bug – it's
because of a concept
called SQL transactions.
16import psycopg2
conn=psycopg2.connect("host=loca
lhost dbname=postgres
user=postgres") cur =
conn.cursor()
cur.execute(""“
CREATE TABLE users(
id integer PRIMARY KEY,
email text,
name text,
address text )
""")
SQL Transactions
● With Postgres, we're dealing
with multiple users who could
be changing the database at
the same time.
● Let's imagine a simple
scenario where we're keeping
track of accounts for different
customers of a bank.
17import psycopg2
conn=psycopg2.connect("host=loca
lhost dbname=postgres
user=postgres") cur =
conn.cursor()
cur.execute("""
CREATE TABLE accounts(
id integer PRIMARY KEY,
name text,
balance float )
""")
SQL Transactions
● Our table looks like this:
● Let’s say Madhav gave 100 rupees to Sanjib. We could model this with
two queries:
UPDATE accounts SET balance=200 WHERE name=“Sanjib";
UPDATE accounts SET balance=100 WHERE name=“Madhav";
18
id name balance
1 Sanjib 100
2 Madhav 200
SQL Transactions
● We remove 100 rupees from Madhav, and add 100 rupees to Sanjib.
What if the second UPDATE statement has an error in it, the database
fails, or another user has a conflicting query?
● The first query would run properly, but the second would fail. That
would result in the following:
19
id name balance
1 Sanjib 200
2 Madhav 200
SQL Transactions
● Sanjib would be credited 100 rupees, but 100 rupees would not be
deducted from Madhav. This would cause the bank to lose money.
● Transactions prevent this type of behaviour by ensuring that all the
queries in a transaction block are executed at the same time. If any of
the transactions fail, the whole group fails, and no changes are made
to the database at all.
● When commit() is called, the PostgreSQL engine will run all the
queries at once.
20
DML: Insert
● Pass data to fill a query placeholders and let
Psycopg perform the correct conversion
● No more SQL injections!
>> "INSERT INTO table_one VALUES(6, 'Sarad Chaudhary', 'Banke
Nepalgunj’)”
21
DML: Insert
Example:
cur = conn.cursor()
insert_query="INSERT INTO table_one VALUES(6, 'Sarad
Chaudhary', 'Banke Nepalgunj’)”
cur.execute(insert_query)
conn.commit()
22
DML: Insert (copy_from() method)
● Method to load a file like .csv into the table of running
database
● Like the execute() method, it is attached to the Cursor object
but it contains different parameters.
● Arguments:
copy_from(f, ‘table_name’, sep=‘,’)
f=file to load(without header), table_name=table name, it should
load into, sep=delimeter
23
copy_from() method
Example:
import psycopg2
conn = psycopg2.connect("dbname='test_database' user='postgres'
host='localhost' password='italy’”)
cur = conn.cursor()
with open('name_list.csv', 'r') as f:
# no need to any other module to load csv
next(f)
# Skip the header row
cur.copy_from(f, 'table_one', sep=',’)
conn.commit()
24
DML: Delete
If you want to pass values to the DELETE statement, you use the
placeholders ( %s) in the DELETE statement and pass input
values to the second parameter of the execute() method.
The DELETE statement with a placeholder for the value of the id
field is as follows:
DELETE FROM test_table WHERE id = %s;
25
DML: Update
Execute the UPDATE statement with the input values
by calling the execute() method of the cursor object.
cur.execute(update_sql, (value1,value2))
26
Thank You

More Related Content

What's hot (20)

Kubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy Agent
CloudOps2005
 
OWASP Top 10 Project
OWASP Top 10 Project
Muhammad Shehata
 
Deteccion de intrusos
Deteccion de intrusos
Yasuara191288
 
Nii sample pt_report
Nii sample pt_report
Chandan Bagai, GWAPT, CEHv8, CCNA
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
WEBSOCKET Protokolünün Derinlemesine İncelenmesi
WEBSOCKET Protokolünün Derinlemesine İncelenmesi
BGA Cyber Security
 
Best practices for Terraform with Vault
Best practices for Terraform with Vault
Mitchell Pronschinske
 
Migrating and modernizing your data estate to Azure with Data Migration Services
Migrating and modernizing your data estate to Azure with Data Migration Services
Microsoft Tech Community
 
ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)
Mydbops
 
Building Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache Kafka
Guido Schmutz
 
Time-Based Blind SQL Injection
Time-Based Blind SQL Injection
matt_presson
 
Openstack live migration
Openstack live migration
ymtech
 
Data Engineering Efficiency @ Netflix - Strata 2017
Data Engineering Efficiency @ Netflix - Strata 2017
Michelle Ufford
 
Threat Hunting with Elastic at SpectorOps: Welcome to HELK
Threat Hunting with Elastic at SpectorOps: Welcome to HELK
Elasticsearch
 
Sql, Sql Injection ve Sqlmap Kullanımı
Sql, Sql Injection ve Sqlmap Kullanımı
BGA Cyber Security
 
Host Header injection - Slides
Host Header injection - Slides
Amit Dubey
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
distributed matters
 
Siber Güvenlik ve Etik Hacking Sunu - 2
Siber Güvenlik ve Etik Hacking Sunu - 2
Murat KARA
 
XSS- an application security vulnerability
XSS- an application security vulnerability
Soumyasanto Sen
 
Kubernetes Security with Calico and Open Policy Agent
Kubernetes Security with Calico and Open Policy Agent
CloudOps2005
 
Deteccion de intrusos
Deteccion de intrusos
Yasuara191288
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
oysteing
 
WEBSOCKET Protokolünün Derinlemesine İncelenmesi
WEBSOCKET Protokolünün Derinlemesine İncelenmesi
BGA Cyber Security
 
Best practices for Terraform with Vault
Best practices for Terraform with Vault
Mitchell Pronschinske
 
Migrating and modernizing your data estate to Azure with Data Migration Services
Migrating and modernizing your data estate to Azure with Data Migration Services
Microsoft Tech Community
 
ProxySQL High Availability (Clustering)
ProxySQL High Availability (Clustering)
Mydbops
 
Building Event Driven (Micro)services with Apache Kafka
Building Event Driven (Micro)services with Apache Kafka
Guido Schmutz
 
Time-Based Blind SQL Injection
Time-Based Blind SQL Injection
matt_presson
 
Openstack live migration
Openstack live migration
ymtech
 
Data Engineering Efficiency @ Netflix - Strata 2017
Data Engineering Efficiency @ Netflix - Strata 2017
Michelle Ufford
 
Threat Hunting with Elastic at SpectorOps: Welcome to HELK
Threat Hunting with Elastic at SpectorOps: Welcome to HELK
Elasticsearch
 
Sql, Sql Injection ve Sqlmap Kullanımı
Sql, Sql Injection ve Sqlmap Kullanımı
BGA Cyber Security
 
Host Header injection - Slides
Host Header injection - Slides
Amit Dubey
 
Programming with Python and PostgreSQL
Programming with Python and PostgreSQL
Peter Eisentraut
 
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
Meet Spilo, Zalando’s HIGH-AVAILABLE POSTGRESQL CLUSTER - Feike Steenbergen
distributed matters
 
Siber Güvenlik ve Etik Hacking Sunu - 2
Siber Güvenlik ve Etik Hacking Sunu - 2
Murat KARA
 
XSS- an application security vulnerability
XSS- an application security vulnerability
Soumyasanto Sen
 

Similar to Connecting and using PostgreSQL database with psycopg2 [Python 2.7] (20)

Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
sachin kumar
 
Database programming
Database programming
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Database connectivity in python
Database connectivity in python
baabtra.com - No. 1 supplier of quality freshers
 
Python database connection
Python database connection
baabtra.com - No. 1 supplier of quality freshers
 
Interface Python with MySQL.pdf
Interface Python with MySQL.pdf
DhirajKumarBiswal
 
Relational Database Access with Python
Relational Database Access with Python
Mark Rees
 
Databases with SQLite3.pdf
Databases with SQLite3.pdf
Deepika,Assistant Professor,PES College of Engineering ,Mandya
 
SQL-Connectivity python for beginners easy explanation with concepts and outp...
SQL-Connectivity python for beginners easy explanation with concepts and outp...
harshitagrawal2608
 
Relational Database Access with Python ‘sans’ ORM
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
PostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
015. Interface Python with sql interface ppt class 12
015. Interface Python with sql interface ppt class 12
Shuvanth
 
Python database access
Python database access
Smt. Indira Gandhi College of Engineering, Navi Mumbai, Mumbai
 
Postgresql
Postgresql
NexThoughts Technologies
 
9 Python programming notes for ktu physics and computer application semester 4
9 Python programming notes for ktu physics and computer application semester 4
ebindboby1
 
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
HistoryScienceWorld
 
SQLite 3 chapter 4 BCA Notes Python NEP syllabus
SQLite 3 chapter 4 BCA Notes Python NEP syllabus
Subrahmanya6
 
python db connection samples and program
python db connection samples and program
usha raj
 
AmI 2015 - Databases in Python
AmI 2015 - Databases in Python
Fulvio Corno
 
Database Connectivity using Python and MySQL
Database Connectivity using Python and MySQL
devsuchaye
 
24. SQL .pdf
24. SQL .pdf
Bhavya103897
 
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
Psycopg2 postgres python DDL Operaytions (select , Insert , update, create ta...
sachin kumar
 
Interface Python with MySQL.pdf
Interface Python with MySQL.pdf
DhirajKumarBiswal
 
Relational Database Access with Python
Relational Database Access with Python
Mark Rees
 
SQL-Connectivity python for beginners easy explanation with concepts and outp...
SQL-Connectivity python for beginners easy explanation with concepts and outp...
harshitagrawal2608
 
Relational Database Access with Python ‘sans’ ORM
Relational Database Access with Python ‘sans’ ORM
Mark Rees
 
PostgreSQL- An Introduction
PostgreSQL- An Introduction
Smita Prasad
 
015. Interface Python with sql interface ppt class 12
015. Interface Python with sql interface ppt class 12
Shuvanth
 
9 Python programming notes for ktu physics and computer application semester 4
9 Python programming notes for ktu physics and computer application semester 4
ebindboby1
 
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
PYTHON_DATABASE_CONNECTIVITY_for_class_12.pptx
HistoryScienceWorld
 
SQLite 3 chapter 4 BCA Notes Python NEP syllabus
SQLite 3 chapter 4 BCA Notes Python NEP syllabus
Subrahmanya6
 
python db connection samples and program
python db connection samples and program
usha raj
 
AmI 2015 - Databases in Python
AmI 2015 - Databases in Python
Fulvio Corno
 
Database Connectivity using Python and MySQL
Database Connectivity using Python and MySQL
devsuchaye
 
Ad

Recently uploaded (20)

A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
A Comprehensive Investigation into the Accuracy of Soft Computing Tools for D...
Journal of Soft Computing in Civil Engineering
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
4th International Conference on Computer Science and Information Technology (...
4th International Conference on Computer Science and Information Technology (...
ijait
 
Structural Design for Residential-to-Restaurant Conversion
Structural Design for Residential-to-Restaurant Conversion
DanielRoman285499
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
OCS Group SG - HPHT Well Design and Operation - SN.pdf
OCS Group SG - HPHT Well Design and Operation - SN.pdf
Muanisa Waras
 
First Come First Serve Scheduling in real time operating system.pptx
First Come First Serve Scheduling in real time operating system.pptx
KavitaBagewadi2
 
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
02 - Ethics & Professionalism - BEM, IEM, MySET.PPT
SharinAbGhani1
 
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
FINAL 2013 Module 20 Corrosion Control and Sequestering PPT Slides.pptx
kippcam
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
Week 6- PC HARDWARE AND MAINTENANCE-THEORY.pptx
dayananda54
 
Flow Chart Proses Bisnis prosscesss.docx
Flow Chart Proses Bisnis prosscesss.docx
rifka575530
 
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Rigor, ethics, wellbeing and resilience in the ICT doctoral journey
Yannis
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
chemistry investigatory project for class 12
chemistry investigatory project for class 12
Susis10
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Ad

Connecting and using PostgreSQL database with psycopg2 [Python 2.7]

  • 1. A presentation on psycopg2 python module by Dinesh Neupane [@neupanedinesh_] | Kathmandu University
  • 2. Psycopg2 A python driver for PostGreSQL PostgreSQL Python Script
  • 3. Background  PostGreSQL database adapter for Python Programming language  Version 2 : original code completely rewritten for better styling of classes for connection and cursor  Postgres is type sensitive so we have to declare types on each of our columns. 1
  • 4. Background ● Robust engine that is implemented as a server rather than a single file. As a server, Postgres accepts connections from clients who can request a SELECT, INSERT, or any other type of SQL query. ● This type of a design is called a client-server model, where clients can interact with the server. 2
  • 5. Requirements  PostGreSQL 9.3 or higher  Python 2.0 or higher 3
  • 6. Installation ● Install python package installer (pip) ● For windows machine (to install package with all it’s dependencies) pip install psycopg2 4
  • 7. Classes and Methods ● Connect() Create a new database connection and returns a new connection instance connect("dbname=‘test_database’user=‘postgres'host='localhost' password=‘italy'") 5
  • 8. Connection Parameters The basic connection parameters are: ● dbname – the database name (database is a deprecated alias) user – user name used to authenticate ● password – password used to authenticate ● host – database host address ● port – connection port number 6
  • 9. Connection Example Example: import psycopg2 try: conn = psycopg2.connect("dbname='test_database' user='postgres' host='localhost' password='italy’”) print "Connection Sucessful !! " except: print "unable to connect to the database" 7
  • 10. Cursor() Cursors are created by the connection.cursor() method: they are bound to the connection for the entire lifetime and all the commands are executed in the context of the database session wrapped by the connection. 8
  • 11. Cursor() The class cursor allows interaction with the database: ● Send commands to the database using using methods like execute() and executemany() ● Retrieve data from the database by iteration or using methods such as fetchone(), fetchmany() or fetchall() ● cursor.description to get the metadata 9
  • 12. Cursor() ● mogrify() - Return a query string after arguments binding. The string returned is exactly the one that would be sent to the database running the execute() method or similar. Example: >>> cur.mogrify("INSERT INTO table_one VALUES(6, 'Sarad Chaudhary', 'Banke Nepalgunj’)”) "INSERT INTO table_one VALUES(6, 'Sarad Chaudhary', 'Banke Nepalgunj’)” 10
  • 13. Create and Drop table CREATE TABLE student ( student_id INTEGER UNIQUE, student_name VARCHAR(50), phone CHAR(10), birth_date DATE, ); This creates a new table costumers with different attribute fields. 11
  • 14. Adaptation of Python Values to SQL Types Python PostgreSQL None Null Bool Bool Float Real, Double Int, Long Smallint, integer, bigint Decimal Numeric Str Varchar List Array 12
  • 15. Closing a database session Make the changes to the database persistent ● Conn.commit() Close the communication with database ● Cur.close() ● Conn.close() 13
  • 16. Create and Drop table The student table is created in the database that you are connected to. [first schema in search path] If you want the table to be created in some other schema, you can prefix the table name with the schema qualifier, for example: CREATE TABLE another_schema.student( ... ); 14
  • 17. Create and Drop table Dropping a table is much easier than creating a table. The syntax for the DROP TABLE command is: DROP TABLE student; The rentals table existed in some schema other than your current schema, you would qualify the table name: DROP TABLE other_schema.student; 15
  • 18. SQL Transactions ● If you check the postgres database now you would notice that there actually isn't a users table inside it. ● This isn't a bug – it's because of a concept called SQL transactions. 16import psycopg2 conn=psycopg2.connect("host=loca lhost dbname=postgres user=postgres") cur = conn.cursor() cur.execute(""“ CREATE TABLE users( id integer PRIMARY KEY, email text, name text, address text ) """)
  • 19. SQL Transactions ● With Postgres, we're dealing with multiple users who could be changing the database at the same time. ● Let's imagine a simple scenario where we're keeping track of accounts for different customers of a bank. 17import psycopg2 conn=psycopg2.connect("host=loca lhost dbname=postgres user=postgres") cur = conn.cursor() cur.execute(""" CREATE TABLE accounts( id integer PRIMARY KEY, name text, balance float ) """)
  • 20. SQL Transactions ● Our table looks like this: ● Let’s say Madhav gave 100 rupees to Sanjib. We could model this with two queries: UPDATE accounts SET balance=200 WHERE name=“Sanjib"; UPDATE accounts SET balance=100 WHERE name=“Madhav"; 18 id name balance 1 Sanjib 100 2 Madhav 200
  • 21. SQL Transactions ● We remove 100 rupees from Madhav, and add 100 rupees to Sanjib. What if the second UPDATE statement has an error in it, the database fails, or another user has a conflicting query? ● The first query would run properly, but the second would fail. That would result in the following: 19 id name balance 1 Sanjib 200 2 Madhav 200
  • 22. SQL Transactions ● Sanjib would be credited 100 rupees, but 100 rupees would not be deducted from Madhav. This would cause the bank to lose money. ● Transactions prevent this type of behaviour by ensuring that all the queries in a transaction block are executed at the same time. If any of the transactions fail, the whole group fails, and no changes are made to the database at all. ● When commit() is called, the PostgreSQL engine will run all the queries at once. 20
  • 23. DML: Insert ● Pass data to fill a query placeholders and let Psycopg perform the correct conversion ● No more SQL injections! >> "INSERT INTO table_one VALUES(6, 'Sarad Chaudhary', 'Banke Nepalgunj’)” 21
  • 24. DML: Insert Example: cur = conn.cursor() insert_query="INSERT INTO table_one VALUES(6, 'Sarad Chaudhary', 'Banke Nepalgunj’)” cur.execute(insert_query) conn.commit() 22
  • 25. DML: Insert (copy_from() method) ● Method to load a file like .csv into the table of running database ● Like the execute() method, it is attached to the Cursor object but it contains different parameters. ● Arguments: copy_from(f, ‘table_name’, sep=‘,’) f=file to load(without header), table_name=table name, it should load into, sep=delimeter 23
  • 26. copy_from() method Example: import psycopg2 conn = psycopg2.connect("dbname='test_database' user='postgres' host='localhost' password='italy’”) cur = conn.cursor() with open('name_list.csv', 'r') as f: # no need to any other module to load csv next(f) # Skip the header row cur.copy_from(f, 'table_one', sep=',’) conn.commit() 24
  • 27. DML: Delete If you want to pass values to the DELETE statement, you use the placeholders ( %s) in the DELETE statement and pass input values to the second parameter of the execute() method. The DELETE statement with a placeholder for the value of the id field is as follows: DELETE FROM test_table WHERE id = %s; 25
  • 28. DML: Update Execute the UPDATE statement with the input values by calling the execute() method of the cursor object. cur.execute(update_sql, (value1,value2)) 26