SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
MODULE 9: SQLITE AND PYTHON (A BRIEF
INTRODUCTION)
SQL and Scripting Training
(C) 2020-2021 Highervista, LLC 1
2
TOPICS
Introduction to Python and SQLite
Import and Connect
The Cursor and Methods within the Cursor
12/12/20 (C) 2020-2021 Highervista, LLC
INTRODUCTION TO PYTHON
AND SQLITE
SQL and Scripting Training
12/12/20 (C) 2020-2021 Highervista, LLC 3
4
WORKING WITH SQLITE
As you can see from working with SQLite’s CLI or a GUI like
DB Browser for SQLite, maintaining a company’s database
would take quite a bit of expertise and devoted time
Using a programming language like Python (and others) can
automate quite a bit of the tasks necessary to maintain the
database
In addition, with a programming language, you can
(optionally) create a user-specific GUI for the company’s
database
12/12/20 (C) 2020-2021 Highervista, LLC
5
A NOTE ABOUT PYTHON
Your next course will be about the Python Programming Language
We will not be covering Python in any detail in this module
Our goal with this module is to introduce general Python concepts, as they
relate to SQLite
More detail will be presented in the Python course, so please consider this
module as a ‘conceptual’ (first pass) overview of Python and SQLite
For the SQL assessment, Python will not be a part of the assessment
12/12/20 (C) 2020-2021 Highervista, LLC
6
USING PYTHON
Assuming that you have a database created (as we’ve
done in this course), you will need to have the
programming language (like Python) connect to the
database.
This ‘connection’ is the link between an existing
database and the Python programming language.
12/12/20 (C) 2020-2021 Highervista, LLC
IMPORT AND CONNECT SQL and Scripting Training
12/12/20 (C) 2020-2021 Highervista, LLC 7
8
What is Needed
In Python, you will need the sqlite3
module (the same one that we used)
The sqlite3 package is ‘imported’ into
Python by the use of the import
command.
After sqlite3 is imported, a
connection between Python and the
specific sqlite3 database must be
made so that Python can access the
specific database.
Python
CONNECTING PYTHON TO AN
SQLITE DATABASE
9
ONCE CONNECTED – CURSOR ‘OBJECT’
Once the database is successfully connected
to Python, a cursor object is established.
A cursor object is a way that Python can
perform actions on the database.
Within the cursor object, there is an
execute() method that allows actions to be
performed on the Sqlite3 database.
For example, the screen image shows the
cursor connection and the ability through the
connection to run a SELECT command. We’ve
done quite a few SELECT commands in our
course.
Establish a connection, then use a cursor object.
The execution of the SELECT moves the ‘result set’
from the query to a location where the data can be
worked on by the Python program.
12/12/20 (C) 2020-2021 Highervista, LLC
10
USING FETCHONE()
Now that a SELECT command was
executed (a SELECT *), we can get one
specific row by using the fetchone()
command.
With fetchone(), we can make changes to
the one item that we ‘fetched.’
Any changes to the fetched record can be
made to the particular record by Python,
based on the decisions in the program.
fetchone() method call
Results of fetchone() method call to example database
12/12/20 (C) 2020-2021 Highervista, LLC
11
PYTHON
Python can ‘seed’ (or set up) the connection and, within the cursor object,
can also set up the particular methods that can be used to maintain the
database.
This avoids an individual who works daily with the company’s database
from knowing the specifics about connection objects and methods.
We leave this knowledge for the programmer to code, test and turn over
to the user.
12/12/20 (C) 2020-2021 Highervista, LLC
THE CURSOR AND METHODS
WITHIN THE CURSOR
SQL and Scripting Training
12/12/20 (C) 2020-2021 Highervista, LLC 12
13
USING THE FETCHALL() METHOD
The fetchall() methods returns all records that can
be worked on (modified) in the Python program.
Depending on the database size, this can be a
huge amount of data.
For our example database, we have a smaller set
of data.
fetchall() method embedded within print command
Result set
12/12/20 (C) 2020-2021 Highervista, LLC
14
PYTHON, EXAMPLE INSERT OF 1 RECORD
Python can be used to insert records into a
specific Sqlite3 table.
Here’s an example where records are
inserted into the consumers table by Python
(first line).
The second line prints the record.
Using the cursor, do an INSERT into a Sqlite3 table on
a previously connected database.
Results of the INSERT printed out.
12/12/20 (C) 2020-2021 Highervista, LLC
15
PYTHON, EXAMPLE OF A LIST OF INSERTED RECORDS
We can INSERT multiple records into
the database using Python.
Python used to set up multiple
records that are INSERTed
Print verification
12/12/20 (C) 2020-2021 Highervista, LLC
16
COMMIT
The cursor that is established between the Python
program and Sqlite3 helps facilitate the work
between the programming language and the
database.
The changes are not made until the transactions
are committed.
The commit() method is part of the connection
object that was first setup to connect the database
to the programming language.
The commit() method of the connection
object makes the data ‘persistent’ in
the database.
12/12/20 (C) 2020-2021 Highervista, LLC
17
VERIFYING THE COMMIT
Once the Python program selects the
data to be changed (modified or
inserted), and the commit is completed
successfully, you can use a tool like the
DB Browser for SQLite to view the
data on the database.
12/12/20 (C) 2020-2021 Highervista, LLC
PYTHON/SQLITE SUMMARY SQL and Scripting Training
12/12/20 (C) 2020-2021 Highervista, LLC 18
19
PYTHON AND SQLITE SUMMARY
You can connect an SQLite database to a Python
program by using a connection object.
The connection object sets up a cursor, where work by
Python can be done on the database.
The cursor has several ‘methods’ to get data from the
database and to place data back into the database,
once Python code is executed on the particular
database.
A commit is required to ensure that the changes are
‘persistent’ on the database.
12/12/20 (C) 2020-2021 Highervista, LLC
20
REFERENCE
McFarland, R. (2020). Published Articles: Ron McFarland.
Retrieved December 03, 2020, from https://medium.com/@highervista
12/12/20 (C) 2020-2021 Highervista, LLC
21
INTRODUCTION
Ron McFarland
Technologist, Educator
Source: Microsoft Images
22
ABOUT THIS COURSE
This course is distributed free. I use several
sources. But importantly, I use the book noted
on the next slide.
If you are using these PowerPoints, please
attribute Highervista, LLC and me (Ron
McFarland). IN ADDITION, please attribute
the author noted on the next slide, as the
author’s textbook provides essential
information for this course.
Source: Microsoft Images
23
INTRODUCTION
This course is offered to you free. HOWEVER, please
purchase the following book, as it is a primary resource for
this course. I do not make any $ from this course or this
book. So, since a handful of good content is derived from the
following text, please support this author!
Title: SQL Quickstart Guide
Author: Walter Shields
Available: Amazon, B&N, and through ClydeBank media
website at:
https://www.clydebankmedia.com/books/programming-
tech/sql-quickstart-guide

Más contenido relacionado

La actualidad más candente

Ado.net session01
Ado.net session01Ado.net session01
Ado.net session01
Niit Care
 
Access lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAccess lesson 02 Creating a Database
Access lesson 02 Creating a Database
Aram SE
 

La actualidad más candente (20)

Dbms important questions and answers
Dbms important questions and answersDbms important questions and answers
Dbms important questions and answers
 
Sq lite module7
Sq lite module7Sq lite module7
Sq lite module7
 
Ch 9 S Q L
Ch 9  S Q LCh 9  S Q L
Ch 9 S Q L
 
MIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome MeasuresMIS5101 WK10 Outcome Measures
MIS5101 WK10 Outcome Measures
 
BIS 245 OUTLET Achievement Education--bis245outlet.com
BIS 245 OUTLET Achievement Education--bis245outlet.comBIS 245 OUTLET Achievement Education--bis245outlet.com
BIS 245 OUTLET Achievement Education--bis245outlet.com
 
BIS 245 OUTLET Inspiring Innovation--bis245outlet.com
 BIS 245 OUTLET Inspiring Innovation--bis245outlet.com BIS 245 OUTLET Inspiring Innovation--bis245outlet.com
BIS 245 OUTLET Inspiring Innovation--bis245outlet.com
 
BIS 245 OUTLET Introduction Education--bis245outlet.com
BIS 245 OUTLET Introduction Education--bis245outlet.comBIS 245 OUTLET Introduction Education--bis245outlet.com
BIS 245 OUTLET Introduction Education--bis245outlet.com
 
BIS 245 HOMEWORK Lessons in Excellence--bis245homework.com
BIS 245 HOMEWORK Lessons in Excellence--bis245homework.comBIS 245 HOMEWORK Lessons in Excellence--bis245homework.com
BIS 245 HOMEWORK Lessons in Excellence--bis245homework.com
 
BIS 245 HOMEWORK Redefined Education--bis245homework.com
BIS 245 HOMEWORK Redefined Education--bis245homework.comBIS 245 HOMEWORK Redefined Education--bis245homework.com
BIS 245 HOMEWORK Redefined Education--bis245homework.com
 
BIS 245 HOMEWORK Become Exceptional--bis245homework.com
BIS 245 HOMEWORK Become Exceptional--bis245homework.comBIS 245 HOMEWORK Become Exceptional--bis245homework.com
BIS 245 HOMEWORK Become Exceptional--bis245homework.com
 
BIS 245 HOMEWORK Introduction Education--bis245homework.com
BIS 245 HOMEWORK Introduction Education--bis245homework.comBIS 245 HOMEWORK Introduction Education--bis245homework.com
BIS 245 HOMEWORK Introduction Education--bis245homework.com
 
Ado.net session01
Ado.net session01Ado.net session01
Ado.net session01
 
BIS 245 Lessons in Excellence / bis245.com
BIS 245 Lessons in Excellence / bis245.comBIS 245 Lessons in Excellence / bis245.com
BIS 245 Lessons in Excellence / bis245.com
 
BIS 245 Redefined Education--bis245.com
BIS 245 Redefined Education--bis245.comBIS 245 Redefined Education--bis245.com
BIS 245 Redefined Education--bis245.com
 
6 database
6 database 6 database
6 database
 
Dbms narrative question answers
Dbms narrative question answersDbms narrative question answers
Dbms narrative question answers
 
ch4
ch4ch4
ch4
 
Database queries
Database queriesDatabase queries
Database queries
 
Viva voce
Viva voceViva voce
Viva voce
 
Access lesson 02 Creating a Database
Access lesson 02 Creating a DatabaseAccess lesson 02 Creating a Database
Access lesson 02 Creating a Database
 

Similar a Sq lite module9

Introduction to ado
Introduction to adoIntroduction to ado
Introduction to ado
Harman Bajwa
 
SQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10gSQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10g
Leidy Alexandra
 
Interface python with sql database10.pdf
Interface python with sql database10.pdfInterface python with sql database10.pdf
Interface python with sql database10.pdf
HiteshNandi
 
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
sarofba
 
Lo extraction part 3 extractor logic
Lo extraction   part 3 extractor logicLo extraction   part 3 extractor logic
Lo extraction part 3 extractor logic
JNTU University
 

Similar a Sq lite module9 (20)

Chapter 3: ado.net
Chapter 3: ado.netChapter 3: ado.net
Chapter 3: ado.net
 
Chapter -7.pptx
Chapter -7.pptxChapter -7.pptx
Chapter -7.pptx
 
Oracle Application Containers
Oracle Application ContainersOracle Application Containers
Oracle Application Containers
 
Unit4
Unit4Unit4
Unit4
 
Introduction to ado
Introduction to adoIntroduction to ado
Introduction to ado
 
(CRUD) How To Connect To Microsoft Access Database Insert Update Delete Clear...
(CRUD) How To Connect To Microsoft Access Database Insert Update Delete Clear...(CRUD) How To Connect To Microsoft Access Database Insert Update Delete Clear...
(CRUD) How To Connect To Microsoft Access Database Insert Update Delete Clear...
 
Key projects Data Science and Engineering
Key projects Data Science and EngineeringKey projects Data Science and Engineering
Key projects Data Science and Engineering
 
Key projects Data Science and Engineering
Key projects Data Science and EngineeringKey projects Data Science and Engineering
Key projects Data Science and Engineering
 
2017 10-oow-fma-application-containers-v01-final
2017 10-oow-fma-application-containers-v01-final2017 10-oow-fma-application-containers-v01-final
2017 10-oow-fma-application-containers-v01-final
 
unit 3.docx
unit 3.docxunit 3.docx
unit 3.docx
 
SQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10gSQL Server Integration Services with Oracle Database 10g
SQL Server Integration Services with Oracle Database 10g
 
Microsoft Access Database And C# Windows Forms Tutorial Visual Studio 2010
Microsoft Access Database And C# Windows Forms Tutorial Visual Studio 2010Microsoft Access Database And C# Windows Forms Tutorial Visual Studio 2010
Microsoft Access Database And C# Windows Forms Tutorial Visual Studio 2010
 
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
 
Sqlite3 databases
Sqlite3 databasesSqlite3 databases
Sqlite3 databases
 
Interface python with sql database.pdf
Interface python with sql database.pdfInterface python with sql database.pdf
Interface python with sql database.pdf
 
Interface python with sql database10.pdf
Interface python with sql database10.pdfInterface python with sql database10.pdf
Interface python with sql database10.pdf
 
Visual Basic.Net & Ado.Net
Visual Basic.Net & Ado.NetVisual Basic.Net & Ado.Net
Visual Basic.Net & Ado.Net
 
A
AA
A
 
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
 
Lo extraction part 3 extractor logic
Lo extraction   part 3 extractor logicLo extraction   part 3 extractor logic
Lo extraction part 3 extractor logic
 

Más de Highervista (6)

Sq lite module3
Sq lite module3Sq lite module3
Sq lite module3
 
Cyber security training using virtual labs 3 cs umuc presentation august 2018
Cyber security training using virtual labs 3 cs umuc presentation august 2018Cyber security training using virtual labs 3 cs umuc presentation august 2018
Cyber security training using virtual labs 3 cs umuc presentation august 2018
 
Cyber security for manufacturers umuc cadf-ron mcfarland
Cyber security for manufacturers umuc cadf-ron mcfarlandCyber security for manufacturers umuc cadf-ron mcfarland
Cyber security for manufacturers umuc cadf-ron mcfarland
 
Intro infosec version 2
Intro infosec version 2Intro infosec version 2
Intro infosec version 2
 
How to create a maker space v2 ebook
How to create a maker space v2 ebookHow to create a maker space v2 ebook
How to create a maker space v2 ebook
 
Love and silence v3 scribd slide share
Love and silence v3 scribd slide shareLove and silence v3 scribd slide share
Love and silence v3 scribd slide share
 

Último

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 

Sq lite module9

  • 1. MODULE 9: SQLITE AND PYTHON (A BRIEF INTRODUCTION) SQL and Scripting Training (C) 2020-2021 Highervista, LLC 1
  • 2. 2 TOPICS Introduction to Python and SQLite Import and Connect The Cursor and Methods within the Cursor 12/12/20 (C) 2020-2021 Highervista, LLC
  • 3. INTRODUCTION TO PYTHON AND SQLITE SQL and Scripting Training 12/12/20 (C) 2020-2021 Highervista, LLC 3
  • 4. 4 WORKING WITH SQLITE As you can see from working with SQLite’s CLI or a GUI like DB Browser for SQLite, maintaining a company’s database would take quite a bit of expertise and devoted time Using a programming language like Python (and others) can automate quite a bit of the tasks necessary to maintain the database In addition, with a programming language, you can (optionally) create a user-specific GUI for the company’s database 12/12/20 (C) 2020-2021 Highervista, LLC
  • 5. 5 A NOTE ABOUT PYTHON Your next course will be about the Python Programming Language We will not be covering Python in any detail in this module Our goal with this module is to introduce general Python concepts, as they relate to SQLite More detail will be presented in the Python course, so please consider this module as a ‘conceptual’ (first pass) overview of Python and SQLite For the SQL assessment, Python will not be a part of the assessment 12/12/20 (C) 2020-2021 Highervista, LLC
  • 6. 6 USING PYTHON Assuming that you have a database created (as we’ve done in this course), you will need to have the programming language (like Python) connect to the database. This ‘connection’ is the link between an existing database and the Python programming language. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 7. IMPORT AND CONNECT SQL and Scripting Training 12/12/20 (C) 2020-2021 Highervista, LLC 7
  • 8. 8 What is Needed In Python, you will need the sqlite3 module (the same one that we used) The sqlite3 package is ‘imported’ into Python by the use of the import command. After sqlite3 is imported, a connection between Python and the specific sqlite3 database must be made so that Python can access the specific database. Python CONNECTING PYTHON TO AN SQLITE DATABASE
  • 9. 9 ONCE CONNECTED – CURSOR ‘OBJECT’ Once the database is successfully connected to Python, a cursor object is established. A cursor object is a way that Python can perform actions on the database. Within the cursor object, there is an execute() method that allows actions to be performed on the Sqlite3 database. For example, the screen image shows the cursor connection and the ability through the connection to run a SELECT command. We’ve done quite a few SELECT commands in our course. Establish a connection, then use a cursor object. The execution of the SELECT moves the ‘result set’ from the query to a location where the data can be worked on by the Python program. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 10. 10 USING FETCHONE() Now that a SELECT command was executed (a SELECT *), we can get one specific row by using the fetchone() command. With fetchone(), we can make changes to the one item that we ‘fetched.’ Any changes to the fetched record can be made to the particular record by Python, based on the decisions in the program. fetchone() method call Results of fetchone() method call to example database 12/12/20 (C) 2020-2021 Highervista, LLC
  • 11. 11 PYTHON Python can ‘seed’ (or set up) the connection and, within the cursor object, can also set up the particular methods that can be used to maintain the database. This avoids an individual who works daily with the company’s database from knowing the specifics about connection objects and methods. We leave this knowledge for the programmer to code, test and turn over to the user. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 12. THE CURSOR AND METHODS WITHIN THE CURSOR SQL and Scripting Training 12/12/20 (C) 2020-2021 Highervista, LLC 12
  • 13. 13 USING THE FETCHALL() METHOD The fetchall() methods returns all records that can be worked on (modified) in the Python program. Depending on the database size, this can be a huge amount of data. For our example database, we have a smaller set of data. fetchall() method embedded within print command Result set 12/12/20 (C) 2020-2021 Highervista, LLC
  • 14. 14 PYTHON, EXAMPLE INSERT OF 1 RECORD Python can be used to insert records into a specific Sqlite3 table. Here’s an example where records are inserted into the consumers table by Python (first line). The second line prints the record. Using the cursor, do an INSERT into a Sqlite3 table on a previously connected database. Results of the INSERT printed out. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 15. 15 PYTHON, EXAMPLE OF A LIST OF INSERTED RECORDS We can INSERT multiple records into the database using Python. Python used to set up multiple records that are INSERTed Print verification 12/12/20 (C) 2020-2021 Highervista, LLC
  • 16. 16 COMMIT The cursor that is established between the Python program and Sqlite3 helps facilitate the work between the programming language and the database. The changes are not made until the transactions are committed. The commit() method is part of the connection object that was first setup to connect the database to the programming language. The commit() method of the connection object makes the data ‘persistent’ in the database. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 17. 17 VERIFYING THE COMMIT Once the Python program selects the data to be changed (modified or inserted), and the commit is completed successfully, you can use a tool like the DB Browser for SQLite to view the data on the database. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 18. PYTHON/SQLITE SUMMARY SQL and Scripting Training 12/12/20 (C) 2020-2021 Highervista, LLC 18
  • 19. 19 PYTHON AND SQLITE SUMMARY You can connect an SQLite database to a Python program by using a connection object. The connection object sets up a cursor, where work by Python can be done on the database. The cursor has several ‘methods’ to get data from the database and to place data back into the database, once Python code is executed on the particular database. A commit is required to ensure that the changes are ‘persistent’ on the database. 12/12/20 (C) 2020-2021 Highervista, LLC
  • 20. 20 REFERENCE McFarland, R. (2020). Published Articles: Ron McFarland. Retrieved December 03, 2020, from https://medium.com/@highervista 12/12/20 (C) 2020-2021 Highervista, LLC
  • 22. 22 ABOUT THIS COURSE This course is distributed free. I use several sources. But importantly, I use the book noted on the next slide. If you are using these PowerPoints, please attribute Highervista, LLC and me (Ron McFarland). IN ADDITION, please attribute the author noted on the next slide, as the author’s textbook provides essential information for this course. Source: Microsoft Images
  • 23. 23 INTRODUCTION This course is offered to you free. HOWEVER, please purchase the following book, as it is a primary resource for this course. I do not make any $ from this course or this book. So, since a handful of good content is derived from the following text, please support this author! Title: SQL Quickstart Guide Author: Walter Shields Available: Amazon, B&N, and through ClydeBank media website at: https://www.clydebankmedia.com/books/programming- tech/sql-quickstart-guide