SlideShare a Scribd company logo
1 of 14
CHAPTER-SEVEN
Python SQL Database Access
By: Mikiale T.
1
INTRODUCTION
 Data is retrieved from a database system using the SQL language. Data is
everywhere and software applications use that. Data is either in memory, files or
databases.
 Python has bindings for many database systems including MySQL, Postregsql,
Oracle, Microsoft SQL Server and Maria DB.
 In this chapter you will learn how to use the SQLite database management system
with Python.
2
INTRODUCTION - - -
 SQLite in general is a server-less database that you can use within
almost all programming languages including Python.
 Server-less means there is no need to install a separate server to
work with SQLite so you can connect directly with the database.
 SQLite is a lightweight database that can provide a relational
database management system with zero-configuration because there
is no need to configure or set up anything to use it.
3
SQLite Basics
 There are four basic steps to working with SQLite:
1) Import the sqlite3 package
2) Connect to an existing database, or create a new
database
3) Execute SQL statements on the database
4) Close the database connection
4
Creating Database Connection
 To use SQLite3 in Python, first of all, you will have to import the sqlite3 module
and then create a connection object which will connect us to the database and will
let us execute the SQL statements.
 You can create a connection object using the connect() function:
 When you create a connection with SQLite, that will create a database file
automatically if it doesn’t already exist. That will create a new file with the
name ‘mydatabase.db’.
 This database file is created on disk; we can also create a database in RAM
5
SQLite3 Cursor
 To execute SQLite statements in Python, you need a cursor object.
You can create it using the cursor() method. The SQLite3 cursor is a
method of the connection object.
 To execute the SQLite3 statements, you should establish a
connection at first and then create an object of the cursor using the
connection object as follows:
6
Creating Table and Inserting Values
7
 To create a table in SQLite3, you can use the Create Table query
in the execute() method. Consider the following steps:
1) Create a connection object and Database.
2) From the connection object, create a cursor object.
3) Using the cursor object, call the execute method with create
table query as the parameter.
4) Insert data into table
5) Display data from table
---Cont’d
 To create a table, we can follow a similar pattern and write the following
commands:
8
---Cont’d
 Let’s take a look at how to add data with SQLite in Python to the database
we just created.
 To insert data in a table, we use the INSERT INTO statement. Consider the
following line of code:
9
Updating Table
 To update the table, simply create a connection, then create a cursor object using the connection
and finally use the UPDATE statement in the execute() method.
 We will use the WHERE clause as a condition to select this employee.
10
Fetch(Display) all data
 To fetch the data from a database, we will execute the SELECT statement and then
will use the fetchall() method of the cursor object to store the values into a
variable. After that, we will loop through the variable and print all values.
 The code will be like this:
11
Libraries for Working With Other SQL Databases
 If you have a particular type of SQL database that you’d like to access through Python, most of
the basic syntax is likely to be identical to what you just learned for SQLite.
 However, you’ll need to install an additional package to interact with your database since
SQLite is the only built-in option.
 There are many SQL variants and corresponding Python packages available. A few of the most
commonly used and reliable open-source alternatives to SQLite are:
 pyodbc, which connects to ODBC (Open Database Connection) databases, such as
Microsoft SQL Server
 psycopg2, which connects to the PostgreSQL database
 PyMySQL, which connects to MySQL databases
N.B : One difference between SQLite and other databases—besides the actual syntax of the SQL code, which changes
slightly with most flavors of SQL—is that most databases require a username and password to connect.
12
Review Exercises
1. Create a new database with a table named Roster that has three fields: Name,
Species and IQ. The Name and Species columns should be text fields, and the IQ
column should be an integer field.
2. Insert values to your new table with the following values:
3. Update the Species of Korben Dallas to be Human.
4. Display the names and IQs of everyone in the table classified as Human.
13
14

More Related Content

Similar to Python SQL Database Access Guide

Python SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabasePython SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabaseElangovanTechNotesET
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxsridharu1981
 
Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5John Coggeshall
 
Class 12 CS Ch-16 MySQL PPT.pptx
Class 12 CS Ch-16 MySQL PPT.pptxClass 12 CS Ch-16 MySQL PPT.pptx
Class 12 CS Ch-16 MySQL PPT.pptxDeepaG66
 
Chapter 6 Interface Python with MYSQL.pptx
Chapter 6 Interface Python with MYSQL.pptxChapter 6 Interface Python with MYSQL.pptx
Chapter 6 Interface Python with MYSQL.pptxsarofba
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentalsMadhuri Kavade
 
Databases in Android Application
Databases in Android ApplicationDatabases in Android Application
Databases in Android ApplicationMark Lester Navarro
 
PythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for DatabasePythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for Databasedharawagh9999
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptxvishal choudhary
 
Oracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivityOracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivityrohit vishwakarma
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.netNgeam Soly
 
Introduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIRIntroduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIRPeter Elst
 

Similar to Python SQL Database Access Guide (20)

Python SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite DatabasePython SQite3 database Tutorial | SQlite Database
Python SQite3 database Tutorial | SQlite Database
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptx
 
Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5
 
Class 12 CS Ch-16 MySQL PPT.pptx
Class 12 CS Ch-16 MySQL PPT.pptxClass 12 CS Ch-16 MySQL PPT.pptx
Class 12 CS Ch-16 MySQL PPT.pptx
 
Sqlite
SqliteSqlite
Sqlite
 
Chapter 6 Interface Python with MYSQL.pptx
Chapter 6 Interface Python with MYSQL.pptxChapter 6 Interface Python with MYSQL.pptx
Chapter 6 Interface Python with MYSQL.pptx
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
Sq lite module9
Sq lite module9Sq lite module9
Sq lite module9
 
Sq lite database
Sq lite databaseSq lite database
Sq lite database
 
Databases in Android Application
Databases in Android ApplicationDatabases in Android Application
Databases in Android Application
 
PythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for DatabasePythonDatabaseAPI -Presentation for Database
PythonDatabaseAPI -Presentation for Database
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
Sq lite
Sq liteSq lite
Sq lite
 
Introduction to ado.net
Introduction to ado.netIntroduction to ado.net
Introduction to ado.net
 
Oracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivityOracle to vb 6.0 connectivity
Oracle to vb 6.0 connectivity
 
Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
Ado.net
Ado.netAdo.net
Ado.net
 
Android sq lite-chapter 22
Android sq lite-chapter 22Android sq lite-chapter 22
Android sq lite-chapter 22
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
Introduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIRIntroduction to SQLite in Adobe AIR
Introduction to SQLite in Adobe AIR
 

More from MikialeTesfamariam (11)

traditional cliphers 7-11-12.ppt
traditional cliphers 7-11-12.ppttraditional cliphers 7-11-12.ppt
traditional cliphers 7-11-12.ppt
 
6 KBS_ES.ppt
6 KBS_ES.ppt6 KBS_ES.ppt
6 KBS_ES.ppt
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
 
Chapter - 6.pptx
Chapter - 6.pptxChapter - 6.pptx
Chapter - 6.pptx
 
Chapter - 5.pptx
Chapter - 5.pptxChapter - 5.pptx
Chapter - 5.pptx
 
Chapter - 2.pptx
Chapter - 2.pptxChapter - 2.pptx
Chapter - 2.pptx
 
Chapter - 3.pptx
Chapter - 3.pptxChapter - 3.pptx
Chapter - 3.pptx
 
Chapter - 1.pptx
Chapter - 1.pptxChapter - 1.pptx
Chapter - 1.pptx
 
Python_Functions.pdf
Python_Functions.pdfPython_Functions.pdf
Python_Functions.pdf
 
functions-.pdf
functions-.pdffunctions-.pdf
functions-.pdf
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 

Recently uploaded

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

Python SQL Database Access Guide

  • 1. CHAPTER-SEVEN Python SQL Database Access By: Mikiale T. 1
  • 2. INTRODUCTION  Data is retrieved from a database system using the SQL language. Data is everywhere and software applications use that. Data is either in memory, files or databases.  Python has bindings for many database systems including MySQL, Postregsql, Oracle, Microsoft SQL Server and Maria DB.  In this chapter you will learn how to use the SQLite database management system with Python. 2
  • 3. INTRODUCTION - - -  SQLite in general is a server-less database that you can use within almost all programming languages including Python.  Server-less means there is no need to install a separate server to work with SQLite so you can connect directly with the database.  SQLite is a lightweight database that can provide a relational database management system with zero-configuration because there is no need to configure or set up anything to use it. 3
  • 4. SQLite Basics  There are four basic steps to working with SQLite: 1) Import the sqlite3 package 2) Connect to an existing database, or create a new database 3) Execute SQL statements on the database 4) Close the database connection 4
  • 5. Creating Database Connection  To use SQLite3 in Python, first of all, you will have to import the sqlite3 module and then create a connection object which will connect us to the database and will let us execute the SQL statements.  You can create a connection object using the connect() function:  When you create a connection with SQLite, that will create a database file automatically if it doesn’t already exist. That will create a new file with the name ‘mydatabase.db’.  This database file is created on disk; we can also create a database in RAM 5
  • 6. SQLite3 Cursor  To execute SQLite statements in Python, you need a cursor object. You can create it using the cursor() method. The SQLite3 cursor is a method of the connection object.  To execute the SQLite3 statements, you should establish a connection at first and then create an object of the cursor using the connection object as follows: 6
  • 7. Creating Table and Inserting Values 7  To create a table in SQLite3, you can use the Create Table query in the execute() method. Consider the following steps: 1) Create a connection object and Database. 2) From the connection object, create a cursor object. 3) Using the cursor object, call the execute method with create table query as the parameter. 4) Insert data into table 5) Display data from table
  • 8. ---Cont’d  To create a table, we can follow a similar pattern and write the following commands: 8
  • 9. ---Cont’d  Let’s take a look at how to add data with SQLite in Python to the database we just created.  To insert data in a table, we use the INSERT INTO statement. Consider the following line of code: 9
  • 10. Updating Table  To update the table, simply create a connection, then create a cursor object using the connection and finally use the UPDATE statement in the execute() method.  We will use the WHERE clause as a condition to select this employee. 10
  • 11. Fetch(Display) all data  To fetch the data from a database, we will execute the SELECT statement and then will use the fetchall() method of the cursor object to store the values into a variable. After that, we will loop through the variable and print all values.  The code will be like this: 11
  • 12. Libraries for Working With Other SQL Databases  If you have a particular type of SQL database that you’d like to access through Python, most of the basic syntax is likely to be identical to what you just learned for SQLite.  However, you’ll need to install an additional package to interact with your database since SQLite is the only built-in option.  There are many SQL variants and corresponding Python packages available. A few of the most commonly used and reliable open-source alternatives to SQLite are:  pyodbc, which connects to ODBC (Open Database Connection) databases, such as Microsoft SQL Server  psycopg2, which connects to the PostgreSQL database  PyMySQL, which connects to MySQL databases N.B : One difference between SQLite and other databases—besides the actual syntax of the SQL code, which changes slightly with most flavors of SQL—is that most databases require a username and password to connect. 12
  • 13. Review Exercises 1. Create a new database with a table named Roster that has three fields: Name, Species and IQ. The Name and Species columns should be text fields, and the IQ column should be an integer field. 2. Insert values to your new table with the following values: 3. Update the Species of Korben Dallas to be Human. 4. Display the names and IQs of everyone in the table classified as Human. 13
  • 14. 14