×

Welcome to TagMyCode

Please login or create account to add a snippet.
1
0
 
0
Language: Java
Posted by: Hafiz Waleed Hussain
Added: Jul 20, 2013 4:04 AM
Modified: Jul 21, 2013 7:08 AM
Views: 1893
  1. public class DBConstructor {
  2.  
  3.         public static final String DB_NAME = "-----";
  4.         public static final int DB_VERSION = 1;
  5.         private Context mContext = null;
  6.         private DBHelper mDBHelper;
  7.         private SQLiteDatabase db;
  8.  
  9.         public DBConstructor(Context context) {
  10.                 this.mContext = context;
  11.         }
  12.  
  13.         public DBConstructor open() {
  14.                 this.mDBHelper = new DBHelper(mContext);
  15.                 this.db = mDBHelper.getWritableDatabase();
  16.                 return this;
  17.         }
  18.  
  19.         public void close() {
  20.                 this.mDBHelper.close();
  21.         }
  22.  
  23.         private static class DBHelper extends SQLiteOpenHelper {
  24.  
  25.                 public DBHelper(Context context) {
  26.                         super(context, DB_NAME, null, DB_VERSION);
  27.                 }
  28.  
  29.                 @Override
  30.                 public void onCreate(SQLiteDatabase db) {
  31.        
  32.                         String query2 = "CREATE TABLE ABC (...)";
  33.  
  34.                         db.execSQL(Info.CREATE_TABLE_QUERY);
  35.                         db.execSQL(query2);
  36.                 }
  37.  
  38.                 @Override
  39.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  40.                         db.execSQL(Info.DROP_TABLE_QUERY);
  41.                         db.execSQL("DROP TABLE IF EXISTS ...");
  42.                 }
  43.         }
  44.  
  45. }
  46. // Database Interface
  47. package interfaces;
  48.  
  49. import vos.DBBaseVO;
  50. import android.content.ContentValues;
  51. import android.database.Cursor;
  52.  
  53. public interface DatabaseOperations {
  54.  
  55.         boolean add(DBBaseVO data);
  56.  
  57.         boolean update(String id, ContentValues cv);
  58.  
  59.         boolean delete(String id);
  60.  
  61.         Cursor getAll();
  62.  
  63.         int getNumberOfRows();
  64.  
  65. }
  66.  
  67. // Table Handler class
  68. package daos;
  69.  
  70. import interfaces.DatabaseOperations;
  71. import vos.DBBaseVO;
  72. import vos.InfoVO;
  73. import android.content.ContentValues;
  74. import android.content.Context;
  75. import android.database.Cursor;
  76. import android.database.SQLException;
  77. import android.database.sqlite.SQLiteDatabase;
  78. import android.database.sqlite.SQLiteOpenHelper;
  79. import android.provider.BaseColumns;
  80.  
  81. public class InfoDAO implements DatabaseOperations {
  82.  
  83.         private Context context;
  84.         public static DBHelper dbHelper;
  85.         public static SQLiteDatabase db;
  86.  
  87.         public InfoDAO(Context context) {
  88.                 this.context = context;
  89.         }
  90.  
  91.         public static class Info implements BaseColumns {
  92.  
  93.                 private Info() {
  94.                 }
  95.  
  96.                 static final String TABLE_NAME = "Info";
  97.                 static final String ID = "_id";
  98.                 static final String NAME = "name";
  99.                 static final String DESCRIPTION = "description";
  100.                 static final String LATITUDE = "latitude";
  101.                 static final String LONGITUDE = "longitude";
  102.                 static final String DISTANCE = "distance";
  103.                 static final String SILENT_TIME = "silent_time";
  104.                 static final String CREATE_DATE = "createdate";
  105.  
  106.                 static final String CREATE_TABLE_QUERY = "CREATE TABLE "
  107.                                 + TABLE_NAME
  108.                                 + "( "
  109.                                 + ID
  110.                                 + " INTEGER PRIMARY KEY AUTOINCREMENT,"
  111.                                 + NAME
  112.                                 + " TEXT,"
  113.                                 + DESCRIPTION
  114.                                 + " TEXT,"
  115.                                 + LATITUDE
  116.                                 + " DOUBLE,"
  117.                                 + LONGITUDE
  118.                                 + " DOUBLE,"
  119.                                 + DISTANCE
  120.                                 + " DOUBLE,"
  121.                                 + SILENT_TIME
  122.                                 + " LONG,creation_date TIMESTAMP NOT NULL DEFAULT current_timestamp);";
  123.  
  124.                 static final String DROP_TABLE_QUERY = "DROP TABLE IF EXISTS "
  125.                                 + TABLE_NAME;
  126.         }
  127.  
  128.         public InfoDAO open() throws SQLException {
  129.                 InfoDAO.dbHelper = new DBHelper(this.context);
  130.                 InfoDAO.db = InfoDAO.dbHelper.getWritableDatabase();
  131.  
  132.                 return this;
  133.         }
  134.  
  135.         public void close() {
  136.                 InfoDAO.dbHelper.close();
  137.  
  138.         }
  139.  
  140.         private static class DBHelper extends SQLiteOpenHelper {
  141.  
  142.                 public DBHelper(Context context) {
  143.                         super(context, DBConstructor.DB_NAME, null,
  144.                                         DBConstructor.DB_VERSION);
  145.                 }
  146.  
  147.                 @Override
  148.                 public void onCreate(SQLiteDatabase db) {
  149.  
  150.                 }
  151.  
  152.                 @Override
  153.                 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  154.                 }
  155.  
  156.         }
  157.  
  158.         @Override
  159.         public boolean add(Object infoVO) {
  160.  
  161.                 ContentValues cv = new ContentValues();
  162.                 cv.put(Info.NAME, infoVO.getName());
  163.                 cv.put(Info.DESCRIPTION, infoVO.getDescription());
  164.                 cv.put(Info.LATITUDE, infoVO.getLatitude());
  165.                 cv.put(Info.LONGITUDE, infoVO.getLongitude());
  166.                 cv.put(Info.DISTANCE, infoVO.getDistance());
  167.                 cv.put(Info.SILENT_TIME, infoVO.getSilentTime());
  168.  
  169.                 return InfoDAO.db.insertWithOnConflict(Info.TABLE_NAME, null, cv,
  170.                                 SQLiteDatabase.CONFLICT_IGNORE) > 0;
  171.         }
  172.  
  173.         @Override
  174.         public boolean update(String id, ContentValues cv) {
  175.  
  176.                 return InfoDAO.db.update(Info.TABLE_NAME, cv, Info.ID + "=?",
  177.                                 new String[] { id }) > 0;
  178.         }
  179.  
  180.         @Override
  181.         public boolean delete(String id) {
  182.  
  183.                 return InfoDAO.db.delete(Info.TABLE_NAME, Info.ID + "=?",
  184.                                 new String[] { id }) > 0;
  185.         }
  186.  
  187.         @Override
  188.         public Cursor getAll() {
  189.                 return InfoDAO.db.query(Info.TABLE_NAME, null, null, null, null, null,
  190.                                 null);
  191.         }
  192.  
  193.         @Override
  194.         public int getNumberOfRows() {
  195.                 Cursor c = getAll();
  196.                 int totalRows = c.getCount();
  197.                 c.close();
  198.  
  199.                 return totalRows;
  200.         }
  201. }
  202.  
  203. // Note: Here you create Table handler classes accroding to your number of tables.
  204.  
  205. // Now last step
  206. // In Application class
  207.         @Override
  208.         public void onCreate() {
  209.                 super.onCreate();
  210.                 context = getApplicationContext();
  211.        
  212.                 DBConstructor db = new DBConstructor (this);
  213.                 db.open();
  214.                 db.close();
  215. }
  216.  
  217. // Now any where in Class where you want to use the table u use the below code snippet.
  218.  
  219. InfoDAO db = new InfoDAO(this);
  220. db.open();
  221. // Working code
  222. db.close();