Ma 07
Ma 07
Course
Mobile Applications
Chapter 07
20 /05/2025
Chapter 07
• Introduction
• Defining a schematic
• Creating a database
• Your database will determine how effectively your mobile application can perform its
intended functions.
• They enable a mobile application to store data, search for information and share data
4
Defining a schema
• One of the fundamental principles of SQL databases is the schema: a formal statement
• The schema is reflected in the SQL statements you use to create your database.
• You may find it useful to create a companion class, called a contract class, which
5
Defining a schema
• Is a container for constants that define the names of URIs, tables and columns.
• The contract class lets you use the same constants in all other classes in the same
package.
• This allows you to change a column name in one place and propagate it throughout
your code.
• A good way to organize a contract class is to place global definitions for your entire
database at the root level of the class.
• Then create an inner class for each table. Each inner class lists the columns of the
corresponding table.
6
Defining a schema
import android.provider.BaseColumns;
private LibraryManagerContract() {}
• Transactions are used to ensure that the database is always in a reasonable state.
8
Create a database
Example :
2 3
1 Right
click
9
Create a database
Example :
10
Create a database
package com.si.gestionnaire_bibliotheque;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
} 12
Create a database
• To access your database, instantiate your class, which inherits the SQLiteOpenHelper
class.
• In our example, this is the LibraryManagerDbHelper class.
• the LibraryManagerDbHelper class will handle all types of transactions on the SQLite
database.
db.insert()
ajouterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Retrieves data repository in write mode
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Creates a value map, where the column names are the keys
ContentValues values = new ContentValues();
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_TITLE,
titreLivreEditText.getText().toString());
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_DESCRIPTION,
descriptionLivreEditText.getText().toString());
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_PUBLISHER,
editeurEditText.getText().toString());
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_YEAR,
anneeEditText.getText().toString());
14
Insert data
db.insert()
More
// Inserts the new row, returning the primary key value of the new row
long newRowId = db.insert(LibraryManagerContract.BookEntry.TABLE_NAME,
null, values);
}
});
db.query()
afficherBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Retrieves data repository in read mode
SQLiteDatabase db = dbHelper.getReadableDatabase();
// Filter results WHERE “year” >= 2000
String selection = LibraryManagerContract.BookEntry.COLUMN_NAME_YEAR + " >= ?";
String[] selectionArgs = { "2000" };
db.query()
db.query()
More
});
18
Read data
db.rawQuery()
afficherToutBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<Book> bookList = new ArrayList<>();
// Gets the data repository in write mode
SQLiteDatabase db = dbHelper.getReadableDatabase();
String sql = "SELECT book._id, book.title, book.description, book.publisher, book.year" + "\n" +
"FROM book" + "\n" +
"ORDER BY book.year desc";
Cursor cursor = db.rawQuery(sql, null);
while(cursor.moveToNext()) {
bookList.add(new Book(
cursor.getLong(cursor.getColumnIndexOrThrow("_id")),
cursor.getString(cursor.getColumnIndexOrThrow("title")),
cursor.getString(cursor.getColumnIndexOrThrow("description")),
cursor.getString(cursor.getColumnIndexOrThrow("publisher")),
cursor.getLong(cursor.getColumnIndexOrThrow("year"))
));
} 20
cursor.close();
Read data
db.rawQuery()
More
bookListView.setAdapter(livresAdapter);
}
});
db.update()
mettreJourBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Creates a value map, where the column names are the keys
ContentValues values = new ContentValues();
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_TITLE,
titreLivreEditText.getText().toString());
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_DESCRIPTION,
descriptionLivreEditText.getText().toString());
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_PUBLISHER,
editeurEditText.getText().toString());
values.put(LibraryManagerContract.BookEntry.COLUMN_NAME_YEAR,
anneeEditText.getText().toString());
22
Update data
db.update()
More
});
23
Delete data
db.delete()
supprimerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Defines the 'where' part of the query.
String selection = BaseColumns._ID + " = ?";
// Specifies arguments in placeholder order.
String[] selectionArgs = { idEditText.getText().toString() };
// Issues an SQL statement.
int deletedRows =
db.delete(LibraryManagerContract.BookEntry.TABLE_NAME,
selection,
selectionArgs);
}
});
24
Example
@+id/mettreJourBtn
@+id/ajouterBtn
@+id/supprimerBtn
@+id/afficherBtn
@+id/afficherToutBtn
@+id/bookListView
25
Example
26