01 Introduction To SQLite
01 Introduction To SQLite
Introduction to SQLite
Programming Guides
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
• We will first look at the SQL language in general terms – this language is
independent of any programming language.
• We will then look at how Python implements this language using its own module
called ‘SQLite3’.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
The software that operates on this data is called a database management system
(DBMS)….
….and all database management systems will need to be able to carry out the
following actions:
- Create a database
- Add data to the database
- Delete data from the database
- Edit the data
- Search the database for specific for data
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Table
Record
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
A query is where we search for data in our database. We ask the database
questions and it responds with answers. For example, we might ask our database
for all students who are 14 and the database will respond with a list of all
students that match the ‘age=14’ criteria.
When we write programs that have databases, SQL (structured query language)
is the language we use to create, update, delete and query our program’s
database.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
There are quite a few but the basic / common statements are:
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Example: M students
Would
rs t_na m e F RO produce…
SELECT fi
1: 2:
Example ROM stud
ents Example dents
CT firs t_ na m e F
su rn a me FROM stu
S EL E ame,
S EL E CT first_n
Would Would
produce… produce…
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
3:
Example
FRO M students
S EL E C T *
o d u ce
Would pr
t a b l e…
the entire
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Example:
dents
FROM stu
SELECT * p=’9AB’
rou
WHERE g
od u ce…
Would pr
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Example
Would pr
:
U
od uceS PDATE students
ET surna
me = ‘Go
WHERE o
first_nam die’
e=’Tess’
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
INSERT
Ex ample:
INTO stu
VALUES den
(‘009’, ‘J ts
im’, ‘Sm
ith’, ‘9AB
’, ‘14’)
Would pr
o d uce The SQL in this example will insert a new row
into the table called students, with the values
specified in the brackets. They will be entered
in the order in which they are provided.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Example
DELETE
:
FROM s
WHERE tud
first_nam ents
e=’Jim’
Would pr
o duceThe SQL in this example will delete the row(s)
in the table called students, where the value in
the field called ‘first_name’ matches ‘Jim’.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Would pr
o duceThe SQL in this example will select all row(s)
in the table called students, where the value in
the field called ‘first_name’ starts with the
letter ‘t’.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Would pr
od uce
The DROP statement however, is used to delete either a table or a database’s data along with the
table/database structures.
Example
:
DROP T
ABLE st
udents
Would
p r o d u ce
The SQL in this example will
remove the table ‘students’
from existence.
No more table!
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
SQLite3 is great for creating and manipulating databases that are stored on a local disk
along with a program file. It comes with Python and is easy to work with.
MySQL is far more powerful and uses a server to store databases created so that they can
be accessed from anywhere (online) providing that there is a network connection.
But MySQL is less easy to work with (partly due to the need for a dedicated server) and
so in A-Level we will look at how we can use SQLite3 to create and manipulate
databases.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Similar to files, before we use a database we need to have a file handler in our program that
interacts with the physical database on a disk or in the RAM.
We use the connect() function, as defined in the SQLite3 library. This function will open a
connection to the SQLite database file.
The following code snippet will connect to a database called “new_db”. It will also create the
database file if it is not present.
If the database file is not located in the same folder as the program then we can write out its path.
For example:
*The argument for the function is the file path for the database; notice that we use double backslash ‘\\’ in our file path as single backslash
denotes an escape sequence.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Now that we have a connection to our database and a cursor object, we can execute SQL
statements to create our table and insert data into our tables. We will create a table called
“Students” with properties such as first name, surname, date of birth and form.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Creating and
Manipulating
Data
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Once we have created our cursor object we can issue SQL commands to the database
using the EXECUTE statement.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Otherwise, every time we run our program we will be running the code which
was needed the first time the program was executed which was to create the
database – running this code if the database is already set up will cause data
loss.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
This is an example of the code needed to insert data into the different fields of the newly
created table.
Notice that we are using the version where we do not specify the field names as
we will be inserting information into all columns
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
The example code below shows how it is possible to enter the contents of variables into
the database table using ‘?’ placeholders.
The insert statement is set up like before but where string data was entered, we simply add
a ‘?’. Then, after the SQL statement, we add the variables which we want to add in the
same order and for each of the question marks.
Notice again that we are using the version where we do not specify the field names
as we will be inserting information into all columns
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
The physical files are shown below. Note that this file is not readable by normal programs on the
computer, and therefore if we open this file we will only see symbols.
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Tasks
45 minutes
Reading Data
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Example: M students
Would
rs t_na m e F RO produce…
SELECT fi
1: 2:
Example ROM stud
ents Example dents
CT firs t_ na m e F
su rn a me FROM stu
S EL E ame,
S EL E CT first_n
Would Would
produce… produce…
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
3:
Example
FRO M students
S EL E C T *
o d u ce
Would pr
t a b l e…
the entire
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Example:
dents
FROM stu
SELECT * p=’9AB’
rou
WHERE g
od u ce…
Would pr
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
t he r e a n
b u t i s
Better, w a y ? !
n be t t e r
e ve
www.computerscienceuk.com
ComputerScienceUK Programming Guide – Python & SQLite
Tasks
45 minutes
www.computerscienceuk.com