SQLite Discussion
SQLite Discussion
Fasttrack IT Academy
What is a database?
It is an organized collection of data.
What is SQLite?
It is a relational database management system.
Basics of SQLite
SQLite uses tables to organize related data.
SQLite Table
A database usually contains one or more tables.
Table Columns
They are also called fields or attributes.
Table Rows
They are also called records or tuple.
CREATE TABLE
It is used to create a table in a database.
CREATE TABLE Students ( id INTEGER PRIMARY KEY, Name TEXT, Course TEXT )
CREATE TABLE
id
Name
Course
INSERT
Adds a new record to the table.
INSERT INTO Students ( id, Name, Course) VALUES ( 1, John, Biology)
INSERT
id
Name
Course
John
Biology
SELECT
Used to query the database.
SELECT * FROM Students
SELECT
Using WHERE clause
SELECT * FROM Students WHERE id = 1
SELECT
Given a table with the following records
Id
Name
Course
John
Biology
Maria
Computer Science
Ana
Chemistry
Mark
Engineering
You will get (1, John, Biology) since this is the only row
that matches the condition in the WHERE clause.
WHERE Clause
It is used to filter records.
UPDATE
Used to modify the values of a record.
UPDATE
Given a table with the following records
Id
Name
Course
John
Biology
Maria
Computer Science
Ana
Chemistry
Mark
Engineering
UPDATE
UPDATE Students SET Course = Physics WHERE id = 1
UPDATE
Our new table will look like this.
Id
Name
Course
John
Physics
Maria
Computer Science
Ana
Chemistry
Mark
Engineering
DELETE
This is used to delete rows in a table.
DELETE
Given a table with the following records
Id
Name
Course
John
Biology
Maria
Computer Science
Ana
Chemistry
Mark
Engineering
DELETE
DELETE FROM Students WHERE id = 3
DELETE
Our new table will look like this.
Id
Name
Course
John
Biology
Maria
Computer Science
Mark
Engineering
Project Setup
Create a new project and fill in the following details.
Import SQLite
Select the Project Name -> Build Phases Tab -> Link Binary with
Libraries
Import SQLite
Click + icon.
Search libsqlite3.dylib in popup
window.
UI Setup
Go to Main.storyboard. Setup
the user interface.
Database Setup
The method checkDB looks for an existing
Registration.sql file in the apps documents directory.
It creates one for us if the file does not exist.
It then adds a Students table with 3 columns (id, Name,
Course) after creating the file.
checkDB Method
checkDB Method
SQLite 3 Methods
Before we go any further, we need to discuss the
methods we will use to communicate with the
database.
didPressAdd Method
We pass the following values to sqlite3_prepare_v2:
1. Open database called registration
2. Insert command add_stmt
3. A flag -1 so SQLite3 will read the entire add_stmt
4. Our statement handler &statement
5. nil pointer since no unused portion of add_stmt
didPressAdd Method
We pass the following values to the bind methods:
1. Our prepared statement.
2. An integer corresponding to the placeholder our
binder will replace. Count starts at 1.
3. The value that we will substitute for the
placeholder.
For text binders:
4. A flag -1 so SQLite3 will read the entire input in 3.
5. SQLITE_STATIC typedef since content pointer will not
change
didPressAdd Method
This method reads the inputs from the ID, Name and
Course text fields and saves them in the database.
didPressUpdate Method
This method updates the Name and Course fields of the
entry whose ID matches the ID value in the text field.
didPressSearch Method
This method searches the database for an ID that
matches the input in our search text field.
didPressDelete Method
This method deletes an entry from the database whose
ID matches the input in the search text field.
Database Interaction
Interaction with the database can be narrowed down to
5 steps:
1. Open database (sqlite3_open)
2. Prepare statement (sqlite3_prepare_v2)
3. Execute statement (sqlite3_step)
4. Finalize statement (sqlite3_finalize)
5. Close database (sqlite3_close)
Final App