Sqllite in Android
Sqllite in Android
Example 1:
SQLite is an open-source server-less database engine. SQLite supports transacations and has no configuration required. SQLite adds
powerful data storage to mobile and embedded apps.
package example.SQLiteTest;
import android.app.Activity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
Open a new private SQLiteDatabase associated with this Context's application package. Create the
database file if it doesn't exist.
Parameters
Before creating tables, certain database properties should be set using the following methods,
Append this code to the previous example for creating tables (tbl_countries and tbl_states)..
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
+ "country_name TEXT);";
+ "state_name TEXT);";
db.execSQL(CREATE_TABLE_COUNTRIES);
db.execSQL(CREATE_TABLE_STATES);
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data.
It has no means to return any data (such as the number of affected rows). Instead, you're encouraged to
use insert(String, String, ContentValues), update(String, ContentValues,
String, String[]), et al, when possible.
Parameters
the SQL statement to be executed. Multiple statements separated by semicolons are not
sql
supported.
Throws
Inserting Records:
import android.content.ContentValues;
Use ContentValues instance to create a series of table field to data matchings that will be passed into an insert() method.
values.put("country_name", "India");
1|India
Parameters
Our simple data format is: [row id] [time recorded] [Hello Android Event].
1. An Activity SQLDemo
In this example, the sqldemo activity has four important methods related to our
database;
AddEvent - ability to add a string type event into the database note the use of the
getWritableDatabase() method to the database handler instance,
GetEvents - ability to query database and here note the getReadableDatabase() method
useage together with the startManagingCursor(cursor) and therefore return a cursor
(walker),
ShowEvents - display of the data submitted by GetEvents cursor that was returned and
loop through all the data,
onDestroy - always release database instance back to system;
SQLDemo.java:
package org.example.sqldemo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@Override
public void onDestroy() {
eventsData.close();
}
startManagingCursor(cursor);
return cursor;
}
Database helper:
Handles all the database connections, creation of the necessary table and has additions like
database upgrade information thrown in to code in case of future database upgrades.
package org.example.sqldemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;
// Table name
public static final String TABLE = "events";
// Columns
public static final String TIME = "time";
public static final String TITLE = "title";
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + TABLE + "( " + BaseColumns._ID
+ " integer primary key autoincrement, " + TIME
+ " integer, "
+ TITLE + " text not null);";
Log.d("EventsData", "onCreate: " + sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
if (oldVersion >= newVersion)
return;