Android SQLite Database Tutorial PDF

Document Details

DeliciousNovaculite7475

Uploaded by DeliciousNovaculite7475

Tags

SQLite database Android development Database inspector Android SQLite

Summary

This document provides a tutorial on using SQLite databases in Android applications. It covers the creation, updating, and accessing of databases, including accessing the table, and schema versioning. The document also introduces how to inspect the database using the Database Inspector tool within Android Studio.

Full Transcript

Android DB: SQLite introduction Defining & creating DB Creating a « helper » class to manage database creation, schema version control, and all kind of accesses The « helper » class extends ”SQLiteOpenHelper” Defining DB name and schema version private sta...

Android DB: SQLite introduction Defining & creating DB Creating a « helper » class to manage database creation, schema version control, and all kind of accesses The « helper » class extends ”SQLiteOpenHelper” Defining DB name and schema version private static final String DATABASE_NAME="restaurant.db"; private static final int SCHEMA_VERSION=1; // version of tables schema Creating DB file with Helper constructor public RestaurantDBHelper(Context context) { super(context, DATABASE_NAME, null, SCHEMA_VERSION);} - null CursorFactory instance to use the standard “SQLiteCursor” implementation - if the DB already exist, it is opened rather then overwritten Defining & updating DB Schema Creating DB internal schema public void onCreate(SQLiteDatabase db){ db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, address TEXT, type TEXT, phone TEXT, web TEXT);");} onCreate method is executed just once, the first time the BD is created (upon a call to one of methods : getReadableDatabase, getWritableDatabase) Changing the DB string name will invoque onCreate again Schema version management Android simplify DB schema upgrade, it auto-launches an upgrade method if it found an existent DB with lower schema version. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) Developper must take care of schema versionning by implementing all possibles scenarios For example if some lazy users have Ver.1. of your DB, others have Ver.2. and new user wil get Ver.3. Developper must implement tow scenarios : going from Ver.1. to Ver.3. and going from Ver.2. to Ver.0.3 (without loosing previous DB data) Accessing DB tables Access : ContentValues : a Class to handle values of one table Record Methode put(String columnName, value) Read : Cursor cursor= getReadableDatabase().rawQuery(query,null); Add : insertedRecId = getWritableDatabase().insert(String table, String nullColumnHack, ContentValues values); Delete : delete(String table, String whereClause, String[] whereArgs) Update : update(String table, ContentValues values, String whereClause, String[] whereArgs) Cursor : moveToFirst(), moveToNext(), moveToLast() isAfterLast getLong(int columnIndex), … getInt, getFloat, getString… Browsing SQLite DB Using Database inspector (starting from Android Studio 4.1)

Use Quizgecko on...
Browser
Browser