public class DBConstructor {
public static final String DB_NAME
= "-----";
public static final int DB_VERSION = 1;
private DBHelper mDBHelper;
private SQLiteDatabase db;
public DBConstructor
(Context context
) {
this.mContext = context;
}
public DBConstructor open() {
this.mDBHelper = new DBHelper(mContext);
this.db = mDBHelper.getWritableDatabase();
return this;
}
public void close() {
this.mDBHelper.close();
}
private static class DBHelper extends SQLiteOpenHelper {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query2
= "CREATE TABLE ABC (...)";
db.execSQL(Info.CREATE_TABLE_QUERY);
db.execSQL(query2);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(Info.DROP_TABLE_QUERY);
db.execSQL("DROP TABLE IF EXISTS ...");
}
}
}
// Database Interface
package interfaces;
import vos.DBBaseVO;
import android.content.ContentValues;
import android.database.Cursor;
public interface DatabaseOperations {
boolean add(DBBaseVO data);
boolean update
(String id, ContentValues cv
);
int getNumberOfRows();
}
// Table Handler class
package daos;
import interfaces.DatabaseOperations;
import vos.DBBaseVO;
import vos.InfoVO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class InfoDAO implements DatabaseOperations {
public static DBHelper dbHelper;
public static SQLiteDatabase db;
this.context = context;
}
public static class Info implements BaseColumns {
private Info() {
}
static final String TABLE_NAME
= "Info";
static final String ID
= "_id";
static final String NAME
= "name";
static final String DESCRIPTION
= "description";
static final String LATITUDE
= "latitude";
static final String LONGITUDE
= "longitude";
static final String DISTANCE
= "distance";
static final String SILENT_TIME
= "silent_time";
static final String CREATE_DATE
= "createdate";
static final String CREATE_TABLE_QUERY
= "CREATE TABLE "
+ TABLE_NAME
+ "( "
+ ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ NAME
+ " TEXT,"
+ DESCRIPTION
+ " TEXT,"
+ LATITUDE
+ " DOUBLE,"
+ LONGITUDE
+ " DOUBLE,"
+ DISTANCE
+ " DOUBLE,"
+ SILENT_TIME
+ " LONG,creation_date TIMESTAMP NOT NULL DEFAULT current_timestamp);";
static final String DROP_TABLE_QUERY
= "DROP TABLE IF EXISTS "
+ TABLE_NAME;
}
InfoDAO.dbHelper = new DBHelper(this.context);
InfoDAO.db = InfoDAO.dbHelper.getWritableDatabase();
return this;
}
public void close() {
InfoDAO.dbHelper.close();
}
private static class DBHelper extends SQLiteOpenHelper {
super(context, DBConstructor.DB_NAME, null,
DBConstructor.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@Override
public boolean add
(Object infoVO
) {
ContentValues cv = new ContentValues();
cv.put(Info.NAME, infoVO.getName());
cv.put(Info.DESCRIPTION, infoVO.getDescription());
cv.put(Info.LATITUDE, infoVO.getLatitude());
cv.put(Info.LONGITUDE, infoVO.getLongitude());
cv.put(Info.DISTANCE, infoVO.getDistance());
cv.put(Info.SILENT_TIME, infoVO.getSilentTime());
return InfoDAO.db.insertWithOnConflict(Info.TABLE_NAME, null, cv,
SQLiteDatabase.CONFLICT_IGNORE) > 0;
}
@Override
public boolean update
(String id, ContentValues cv
) {
return InfoDAO.db.update(Info.TABLE_NAME, cv, Info.ID + "=?",
}
@Override
public boolean delete
(String id
) {
return InfoDAO.db.delete(Info.TABLE_NAME, Info.ID + "=?",
}
@Override
return InfoDAO.db.query(Info.TABLE_NAME, null, null, null, null, null,
null);
}
@Override
public int getNumberOfRows() {
int totalRows = c.getCount();
c.close();
return totalRows;
}
}
// Note: Here you create Table handler classes accroding to your number of tables.
// Now last step
// In Application class
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
DBConstructor db = new DBConstructor (this);
db.open();
db.close();
}
// Now any where in Class where you want to use the table u use the below code snippet.
InfoDAO db = new InfoDAO(this);
db.open();
// Working code
db.close();