A template for using SQLite in Android
This code is a template for using SQLite in Android to implement a database:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "database_name";
// Table Name
private static final String TABLE_NAME = "table_name";
// Column names
private static final String COLUMN_ID = "id";
private static final String COLUMN_COLUMN_ONE = "column_one";
private static final String COLUMN_COLUMN_TWO = "column_two";
private static final String COLUMN_COLUMN_THREE = "column_three";
// Create table query
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_COLUMN_ONE + " TEXT,"
+ COLUMN_COLUMN_TWO + " TEXT,"
+ COLUMN_COLUMN_THREE + " TEXT" + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating the table
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
// Upgrading the database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
// Inserting new data into the table
public long insertData(String columnOne, String columnTwo, String columnThree) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_COLUMN_ONE, columnOne);
contentValues.put(COLUMN_COLUMN_TWO, columnTwo);
contentValues.put(COLUMN_COLUMN_THREE, columnThree);
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
return result;
}
// Updating existing data in the table
public int updateData(int id, String columnOne, String columnTwo, String columnThree) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_COLUMN_ONE, columnOne);
contentValues.put(COLUMN_COLUMN_TWO, columnTwo);
contentValues.put(COLUMN_COLUMN_THREE, columnThree);
int result = db.update(TABLE
Deleting data from the table:
// Deleting a data from the table
public int deleteData(int id) {
SQLiteDatabase db = this.getWritableDatabase();
int result = db.delete(TABLE_NAME, COLUMN_ID + " = ?",
new String[]{String.valueOf(id)});
db.close();
return result;
}
Reading data from the table:
// Reading data from the table
public Cursor readData() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return cursor;
}
The above code is a template for using SQLite in Android. In this code, we first create a class `DatabaseHelper` which extends the `SQLiteOpenHelper` class. The `SQLiteOpenHelper` class provides the basic functions required to create, update and delete a database.
We first define the database version, name, table name and column names. The `CREATE_TABLE` query is used to create the table in the database. The `onCreate` method is called when the database is first created, and it executes the `CREATE_TABLE` query. The `onUpgrade` method is called when the database version is updated, and it drops the old table and creates a new table with the updated version.
The `insertData` method is used to insert new data into the table, the `updateData` method is used to update existing data in the table, and the `deleteData` method is used to delete data from the table. The `readData` method is used to read data from the table.