Advertisement
Guest User

DBAdapter

a guest
May 21st, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1.    
  2.  
  3.  
  4. import android.content.ContentValues;
  5. import android.content.Context;
  6. import android.database.Cursor;
  7. import android.database.SQLException;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10. import android.os.Environment;
  11. import android.util.Log;
  12.  
  13. public class FavBaDbAdapter2 {
  14.  
  15.  public static final String KEY_ROWID = "rowid _id";
  16.  public static final String KEY_CODE = "word";
  17.  public static final String KEY_NAME = "content";
  18.  public static final String KEY_TIME = "time";
  19.  public static final String KEY_FAVID = "favid";
  20.  
  21.  
  22.  private static final String TAG = "ItemsDbAdapter";
  23.  private DatabaseHelper mDbHelper;
  24.  private SQLiteDatabase mDb;
  25.  
  26.  
  27.  private static final String DATABASE_NAME = "favtemp";
  28.  private static final String SQLITE_TABLE = "favitems";
  29.  private static final int DATABASE_VERSION = 1;
  30.  
  31.  private final Context mCtx;
  32.  
  33.  private static class DatabaseHelper extends SQLiteOpenHelper {
  34.  
  35.      public DatabaseHelper(final Context context) {
  36.          
  37.          
  38.             super(context, Environment.getExternalStorageDirectory()
  39.                     + File.separator + "Kadict/fav/"
  40.                     + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
  41.         }
  42.  
  43.  
  44. @Override
  45. public void onCreate(SQLiteDatabase db) {
  46.     // TODO Auto-generated method stub
  47.    
  48. }
  49.  
  50.  
  51. @Override
  52. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  53.     // TODO Auto-generated method stub
  54.    
  55. }
  56.  }
  57.  
  58.  public FavBaDbAdapter2(Context ctx) {
  59.   this.mCtx = ctx;
  60.  }
  61.  
  62.  public FavBaDbAdapter2 open() throws SQLException {
  63.   mDbHelper = new DatabaseHelper(mCtx);
  64.   mDb = mDbHelper.getWritableDatabase();
  65.   return this;
  66.  }
  67.  
  68.  public void close() {
  69.   if (mDbHelper != null) {
  70.    mDbHelper.close();
  71.   }
  72.  }
  73.  
  74.  public long createCountry(String code, String name,
  75.    String time, String favid) {
  76.  
  77.   ContentValues initialValues = new ContentValues();
  78.   initialValues.put(KEY_CODE, code);
  79.   initialValues.put(KEY_NAME, name);
  80.   initialValues.put(KEY_TIME, time);
  81.   initialValues.put(KEY_FAVID, favid);
  82.  
  83.  
  84.   return mDb.insert(SQLITE_TABLE, null, initialValues);
  85.  }
  86.  
  87.  public boolean deleteAllItems() {
  88.  
  89.   int doneDelete = 0;
  90.   doneDelete = mDb.delete(SQLITE_TABLE, null , null);
  91.   Log.w(TAG, Integer.toString(doneDelete));
  92.   return doneDelete > 0;
  93.    
  94.  }
  95.  
  96.  public Cursor fetchItemsByName(String inputText) throws SQLException {
  97.   Log.w(TAG, inputText);
  98.   Cursor mCursor = null;
  99.   if (inputText == null  ||  inputText.length () == 0)  {
  100.    mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
  101.      KEY_CODE, KEY_NAME, KEY_TIME, KEY_FAVID},
  102.      null, null, null, null, null);
  103.  
  104.   }
  105.   else {
  106.    mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
  107.      KEY_CODE, KEY_NAME, KEY_TIME},
  108.      KEY_CODE + " LIKE ?",
  109.      new String[] { inputText+"%" }, null, null, null,
  110.      null);
  111.   }
  112.   if (mCursor != null) {
  113.    mCursor.moveToFirst();
  114.   }
  115.   return mCursor; //inputText
  116.  
  117.  }
  118.  
  119.  public Cursor fetchAllItems() {
  120.      
  121.      Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
  122.                 KEY_CODE, KEY_NAME, KEY_TIME, KEY_FAVID},
  123.                 null, null, null, null, null);
  124.  
  125.   if (mCursor != null) {
  126.    mCursor.moveToFirst();
  127.   }
  128.   return mCursor;
  129.  }
  130.  
  131.  
  132.  
  133.  public void insertSomeItems() {
  134.  
  135.  
  136.  }
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement