2. What is SQLite?
SQLite is an Open Source database.
SQLite supports standard relational
database features like (SQL syntax,
transactions and prepared statements)
The database requires limited memory at
runtime (approx. 250 KByte) which makes
it a good candidate from being
embedded into other runtimes.
3. SQLite supports the data types TEXT
(similar to String in Java), INTEGER (similar
to long in Java) and REAL (similar to
double in Java).
SQLite itself does not validate if the types
written to the columns are actually of the
defined type.
4. SQLite in Android
SQLite is embedded into every Android
device.
Using an SQLite database in Android does not
require a setup procedure or administration of
the database.
You only have to define the SQL statements
for creating and updating the database.
Afterwards the database is automatically
managed for you by the Android platform.
5. Creating and updating
Database
To create and upgrade a database in your
Android application you create a subclass of
the SQLiteOpenHelper class.
In this class you need to override the following
methods to create and update your
database.
onCreate() - is called by the framework, if the
database is accessed but not yet created.
onUpgrade() - called, if the database version is
increased in your application code.
6. Reading or writing mode
The SQLiteOpenHelper class provides the
getReadableDatabase() and
getWriteableDatabase() methods to get
access to an SQLiteDatabase object.
getReadableDatabase():Create and/or open a
database (read-only database object will be
returned)
getWriteableDatabase():Create and/or open a
database that will be used for reading and
writing(read-only database object will be
returned)
7. How to create db
public static final String TABLE_USERS = "users";
public static final String USER_ID = "_id";
public static final String USER_AGE = "age";
public static final String USER_EMAIL = "email";
public static final String USER_SEX = "sex";
public static final String USER_NAME = "name";
public static final String USER_PASSWORD =
"password";
8. Create Table
private static final String
CREATE_USERS_TABLE = "create table ”+
TABLE_USERS + "(" + USER_ID
+ " integer primary key autoincrement, " +
USER_NAME+ " text not null, " + USER_EMAIL
+ " text not null, ”+ USER_PASSWORD + " text
not null," + USER_AGE + " text not null,”+
USER_SEX + " text not null);";
9. Fill Override methods
public void onCreate(SQLiteDatabase
database) {
database.execSQL(CREATE_USERS_TA
BLE);
}
public void onUpgrade(SQLiteDatabase db, int
oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "
+ TABLE_USERS);
onCreate(db);
13. interface DAO
public interface DAO {
public List<?> getAllItems();
public Object getItem(int id);
public void updateItem(Object obj);
public void deleteItem(Object obj);
public int insertItem(Object obj);
public SQLiteDatabase openDB();
public void closeDB();
}