Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pixign.healthy.lady.dbs;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.util.Log;
- public abstract class DBA_Base {
- private static final String MYDATABASE_NAME = "hl_db";
- private static final int MYDATABASE_VERSION = 1;
- protected SQLiteHelper sqLiteHelper;
- protected SQLiteDatabase sqLiteDatabase;
- protected Context context;
- public DBA_Base(Context c){
- context = c;
- }
- public DBA_Base openToRead() throws android.database.SQLException {
- sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
- sqLiteDatabase = sqLiteHelper.getReadableDatabase();
- sqLiteHelper.forceIt(sqLiteDatabase);
- return this;
- }
- public DBA_Base openToWrite() throws android.database.SQLException {
- sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
- sqLiteDatabase = sqLiteHelper.getWritableDatabase();
- return this;
- }
- public void close(){
- sqLiteHelper.close();
- }
- public void deleteAll(){
- sqLiteDatabase.delete(getTableName(), null, null);
- }
- public Cursor queryAll() {
- Cursor cursor = sqLiteDatabase.query(getTableName(), getColNames(),
- null, null, null, null, null);
- return cursor;
- }
- public Cursor queryById(int id){
- String c1 = getColNames()[0];
- String[] columns = new String[]{c1};
- Cursor cursor = sqLiteDatabase.query(getTableName(), columns,
- c1 + " = " + id, null, null, null, null);
- return cursor;
- }
- public class SQLiteHelper extends SQLiteOpenHelper {
- public SQLiteHelper(Context context, String name,
- CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
- @Override
- public void onCreate(SQLiteDatabase db) {
- forceIt(db);
- }
- public void forceIt(SQLiteDatabase db){
- Log.e("i", "TABLE BEING CREATED -------------------------------- " + getTableName() );
- db.execSQL(getDBSetup());
- }
- }
- // return a string of table being created
- protected abstract String getDBSetup();
- // return a string containing the desired table name
- protected abstract String getTableName();
- // return a string array of the db collum names
- public abstract String[] getColNames();
- // return a string array of user visible collum names, empty strings for the hidden ones
- public abstract String[] getVisColNames();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement