SHARE
TWEET

Untitled

stsharin Dec 6th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.as400samplecode;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.SQLException;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteOpenHelper;
  9. import android.util.Log;
  10.  
  11. public class CustomersDbAdapter {
  12.  
  13.     public static final String KEY_ROWID = "rowid";
  14.     public static final String KEY_CUSTOMER = "customer";
  15.     public static final String KEY_NAME = "name";
  16.     public static final String KEY_ADDRESS = "address";
  17.     public static final String KEY_ADDRESS1 = "address1";
  18.     public static final String KEY_ADDRESS2 = "address2";
  19.     public static final String KEY_CITY = "city";
  20.     public static final String KEY_STATE = "state";
  21.     public static final String KEY_ZIP = "zipCode";
  22.     public static final String KEY_SEARCH = "searchData";
  23.  
  24.     private static final String TAG = "CustomersDbAdapter";
  25.     private DatabaseHelper mDbHelper;
  26.     private SQLiteDatabase mDb;
  27.  
  28.     private static final String DATABASE_NAME = "CustomerData";
  29.     private static final String FTS_VIRTUAL_TABLE = "CustomerInfo";
  30.     private static final int DATABASE_VERSION = 1;
  31.  
  32.     //Create a FTS3 Virtual Table for fast searches
  33.     private static final String DATABASE_CREATE =
  34.         "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
  35.         KEY_CUSTOMER + "," +
  36.         KEY_NAME + "," +
  37.         KEY_ADDRESS1 + "," +
  38.         KEY_ADDRESS2 + "," +
  39.         KEY_CITY + "," +
  40.         KEY_STATE + "," +
  41.         KEY_ZIP + "," +
  42.         KEY_SEARCH + "," +
  43.         " UNIQUE (" + KEY_CUSTOMER + "));";
  44.  
  45.  
  46.     private final Context mCtx;
  47.  
  48.     private static class DatabaseHelper extends SQLiteOpenHelper {
  49.  
  50.         DatabaseHelper(Context context) {
  51.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  52.         }
  53.  
  54.  
  55.         @Override
  56.         public void onCreate(SQLiteDatabase db) {
  57.             Log.w(TAG, DATABASE_CREATE);
  58.             db.execSQL(DATABASE_CREATE);
  59.         }
  60.  
  61.         @Override
  62.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  63.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  64.                     + newVersion + ", which will destroy all old data");
  65.             db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
  66.             onCreate(db);
  67.         }
  68.     }
  69.  
  70.     public CustomersDbAdapter(Context ctx) {
  71.         this.mCtx = ctx;
  72.     }
  73.  
  74.     public CustomersDbAdapter open() throws SQLException {
  75.         mDbHelper = new DatabaseHelper(mCtx);
  76.         mDb = mDbHelper.getWritableDatabase();
  77.         return this;
  78.     }
  79.  
  80.     public void close() {
  81.         if (mDbHelper != null) {
  82.             mDbHelper.close();
  83.         }
  84.     }
  85.  
  86.  
  87.     public long createCustomer(String customer, String name, String address1, String address2, String city, String state, String zipCode) {
  88.  
  89.         ContentValues initialValues = new ContentValues();
  90.         String searchValue =     customer + " " +
  91.                                 name + " " +
  92.                                 address1 + " " +
  93.                                 city + " " +
  94.                                 state + " " +
  95.                                 zipCode;
  96.         initialValues.put(KEY_CUSTOMER, customer);
  97.         initialValues.put(KEY_NAME, name);
  98.         initialValues.put(KEY_ADDRESS1, address1);
  99.         initialValues.put(KEY_ADDRESS2, address2);
  100.         initialValues.put(KEY_CITY, city);
  101.         initialValues.put(KEY_STATE, state);
  102.         initialValues.put(KEY_ZIP, zipCode);
  103.         initialValues.put(KEY_SEARCH, searchValue);
  104.  
  105.         return mDb.insert(FTS_VIRTUAL_TABLE, null, initialValues);
  106.     }
  107.  
  108.  
  109.     public Cursor searchCustomer(String inputText) throws SQLException {
  110.         Log.w(TAG, inputText);
  111.         String query = "SELECT docid as _id," +
  112.         KEY_CUSTOMER + "," +
  113.         KEY_NAME + "," +
  114.         "(" + KEY_ADDRESS1 + "||" +
  115.         "(case when " + KEY_ADDRESS2 +  "> '' then '\n' || " + KEY_ADDRESS2 + " else '' end)) as " +  KEY_ADDRESS +"," +
  116.         KEY_ADDRESS1 + "," +
  117.         KEY_ADDRESS2 + "," +
  118.         KEY_CITY + "," +
  119.         KEY_STATE + "," +
  120.         KEY_ZIP +
  121.         " from " + FTS_VIRTUAL_TABLE +
  122.         " where " +  KEY_SEARCH + " MATCH '" + inputText + "';";
  123.         Log.w(TAG, query);
  124.         Cursor mCursor = mDb.rawQuery(query,null);
  125.  
  126.         if (mCursor != null) {
  127.             mCursor.moveToFirst();
  128.         }
  129.         return mCursor;
  130.  
  131.     }
  132.  
  133.  
  134.     public boolean deleteAllCustomers() {
  135.  
  136.         int doneDelete = 0;
  137.         doneDelete = mDb.delete(FTS_VIRTUAL_TABLE, null , null);
  138.         Log.w(TAG, Integer.toString(doneDelete));
  139.         return doneDelete > 0;
  140.  
  141.     }
  142.  
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top