10 - Sqlite
10 - Sqlite
Roaa SOLOH
solohrk@rhu.edu.lb
Fall 2022
Objectives
• Introduction to SQLiteDatabase
• Database Creation and Insertion
• Database Querying
• Database Helper Class
Introduction to SQLiteDatabase
What is a Database?
• Oracle
• Microsoft
• SQLServer(powerful) / Access(simple)
• PostgreSQL
• powerful/complex free open-source DB system
• SQLite
• transportable, lightweight free open-source DB system
• MySQL
• simple free open-source database system
• many servers run “LAMP" (Linux, Apache, MySQL, and PHP)
• Wikipedia is run on PHP and MySQL
SQLiteDatabase
• SQLite is a open source SQL database that stores data to a text file on
a device.
• Android comes in with built in SQLite database implementation.
• SQLite supports all the relational database features.
• In order to access this database, you don't need to establish any kind
of connections for it like JDBC,ODBC etc.
• The package android.database.sqlite contains the classes to manage
your own databases
SQL
primary key:
column guaranteed to
be unique for each
row (ID)
Foreign key: key
referring to a primary
key
Join
SELECT column(s)
FROM table1 name1
JOIN table2 name2 ON condition(s)
... JOIN tableN nameN ON condition(s)
WHERE condition;
•execSQL(String sql) or
•execSQL(String sql, Object[] bindArgs)
•These methods are used to update or modify already existing data in
database with or without using bind arguments.
•Example: mydatabase.execSQL("INSERT INTO MyTable
VALUES('admin','admin');");
ContentValues
Use:
ContentValues cvalues = new ContentValues(); cvalues.put("columnName1",
value1); cvalues.put("columnName2", value2);
...
db.insert("tableName", null, cvalues);
ContentValues
•db.insert (String table, String nullColumnHack, ContentValues values)
•table: the table to insert the row into
•nullColumnHack: may be null. SQL doesn't allow inserting an empty
row without naming at least one column name. This parameter provides the
name of a nullable column to explicitly insert a NULL into in the case
where values is empty.
•values: this map contains the initial column values for the row. The keys
should be the column names and the values the column values
•Returns the row ID of the newly inserted row, or -1 if an error occurred
Database Querying
Query Database
• To retrieve data from the database, can use the function rawQuery.
•public Cursor rawQuery (String sql, String[]
selectionArgs)
•sql: the SQL query. The SQL string must not be ; terminated
•selectionArgs: You may include ?s in where clause in the query,
which will be replaced by the values from selectionArgs. The values
will be bound as Strings.
Query Database
Runs the provided SQL and returns a Cursor over the result set.
The Cursor object is positioned before the first entry and can be
moved forward to retrieve the data.
Cursor
@Override
public void onCreate(SQLiteDatabase db)
{ db.execSQL(TABLE_CREATE);
}
onUpdate()
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion != newVersion) {
// Simplest implementation is to drop all
// old tables and recreate them db.execSQL("DROP TABLE IF EXISTS "
+ TABLE_PASSWORDS); db.execSQL("DROP TABLE IF EXISTS "
+ TABLE_USERNAMES);
onCreate(db);
}
}