0% found this document useful (0 votes)
33 views

SQLite Discussion

This document provides an overview of SQLite and how to build an iOS app that interacts with an SQLite database. It discusses what a database is and how SQLite works as a relational database management system. It also covers SQLite concepts like tables, rows, columns, and SQL commands like CREATE TABLE, INSERT, SELECT, UPDATE, DELETE. Finally, it demonstrates how to set up an iOS project to use SQLite, linking the SQLite library and implementing methods for common database operations like adding, updating, searching and deleting data.

Uploaded by

LOLriffic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

SQLite Discussion

This document provides an overview of SQLite and how to build an iOS app that interacts with an SQLite database. It discusses what a database is and how SQLite works as a relational database management system. It also covers SQLite concepts like tables, rows, columns, and SQL commands like CREATE TABLE, INSERT, SELECT, UPDATE, DELETE. Finally, it demonstrates how to set up an iOS project to use SQLite, linking the SQLite library and implementing methods for common database operations like adding, updating, searching and deleting data.

Uploaded by

LOLriffic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

SQLite Discussion

Fasttrack IT Academy

What is a database?
It is an organized collection of data.

We use Database Management Systems (DBMS)


to interact with a database.
We use SQLite DBMS in iOS Development.

What is SQLite?
It is a relational database management system.

It is contained in a compact C programming


library.
It does not require a separate server process
since it reads and writes directly to a single disk
file.

Basics of SQLite
SQLite uses tables to organize related data.

SQLite uses Structured Query Language (SQL) to


manipulate data.

SQLite Table
A database usually contains one or more tables.

Each table contains columns and rows.

Table Columns
They are also called fields or attributes.

They are a set of data values that contain a


single type.
They define the data in a table.

Table Rows
They are also called records or tuple.

They represent a single structured data in a


table.

Structured Query Language


It is a computer language designed for managing
data in relational DBMS.
It uses statements to perform Create Table,
Select, Update, Delete and Insert commands.

CREATE TABLE
It is used to create a table in a database.
CREATE TABLE Students ( id INTEGER PRIMARY KEY, Name TEXT, Course TEXT )

Students table name


id, Name, Course column names
INTEGER, TEXT data types

CREATE TABLE
id

Name

Course

INSERT
Adds a new record to the table.
INSERT INTO Students ( id, Name, Course) VALUES ( 1, John, Biology)

Students name of table


id, Name, Course columns of Students table

1, John, Biology values to insert in table columns

INSERT
id

Name

Course

John

Biology

SELECT
Used to query the database.
SELECT * FROM Students

Returns all rows from Students table.

SELECT
Using WHERE clause
SELECT * FROM Students WHERE id = 1

Returns all rows from Students table that matches the


condition in the WHERE clause

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.

Only those records that fulfill a certain condition


are extracted.

UPDATE
Used to modify the values of a record.

A WHERE clause is usually used to limit the


modification to a specific set of data.

UPDATE
Given a table with the following records
Id

Name

Course

John

Biology

Maria

Computer Science

Ana

Chemistry

Mark

Engineering

If we want to update the course of a specific


student, we write the following

UPDATE
UPDATE Students SET Course = Physics WHERE id = 1

We change the course of the student whose id


number matches the WHERE clause.

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.

A WHERE clause is usually used to limit the data


to be deleted.

DELETE
Given a table with the following records
Id

Name

Course

John

Biology

Maria

Computer Science

Ana

Chemistry

Mark

Engineering

If we want to delete a specific record, we write


the following

DELETE
DELETE FROM Students WHERE id = 3

We delete the Student record whose id matches


the WHERE clause.

DELETE
Our new table will look like this.
Id

Name

Course

John

Biology

Maria

Computer Science

Mark

Engineering

iOS Database Integration

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.

Select library and click Add.

UI Setup
Go to Main.storyboard. Setup
the user interface.

4 labels ID, Name, Course,


Status
4 text fields ID, Name,
Course, Search

4 buttons Add, Update,


Search, Delete

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

Prepares SQL command for creating a table Students with 3


columns (id, Name, Course).

Executes sql_create_stmt. Passes the open database, the SQL


command and an error handler.

checkDB Method

Closes the database connection.

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

You might also like