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
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