我正在执行搜索操作。我创建了一个 DBadapter。我只能手动插入值并执行搜索。但我想从 sqlite 文件中插入数据。如何继续?我应该将该 sqlite 文件放在 assets 文件夹中吗?请告诉我如何进行。基本上我想从 sqlite 文件中搜索数据。
这是我的主要活动代码:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
searchView = (SearchView) findViewById(R.id.search);
searchView.setIconifiedByDefault(false);
searchView.setOnQueryTextListener(this);
searchView.setOnCloseListener(this);
mListView = (ListView) findViewById(R.id.list);
AssetManager manager = this.getAssets();
mDbHelper = new CustomersDbAdapter(this);
mDbHelper.open();
mDbHelper.deleteAllCustomers();
//Add some Customer data as a sample
mDbHelper.createCustomer("PIZZA1", "Piz", "1107 West Ada", "", "Los Angeles");
mDbHelper.createCustomer("PIZZA2", "Pizza Hut", "1562 West ", "", "Los Angeles");
mDbHelper.createCustomer("SUB4", "Subway", "504 West ", "", "Los Angeles");
}
这是 DBAdapter:
public class CustomersDbAdapter
{
public static final String KEY_ROWID = "rowid";
public static final String KEY_CUSTOMER = "customer";
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_ADDRESS1 = "address1";
public static final String KEY_ADDRESS2 = "address2";
public static final String KEY_CITY = "city";
public static final String KEY_SEARCH = "searchData";
private static final String TAG = "CustomersDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "CustomerData";
private static final String FTS_VIRTUAL_TABLE = "CustomerInfo";
private static final int DATABASE_VERSION = 1;
//Create a FTS3 Virtual Table for fast searches
private static final String DATABASE_CREATE =
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + " USING fts3(" +
KEY_CUSTOMER + "," +
KEY_NAME + "," +
KEY_ADDRESS1 + "," +
KEY_ADDRESS2 + "," +
KEY_CITY + "," +
//KEY_STATE + "," +
//KEY_ZIP + "," +
KEY_SEARCH + "," +
" UNIQUE (" + KEY_CUSTOMER + "));";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
//db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
onCreate(db);
}
}
public CustomersDbAdapter(Context ctx)
{
this.mCtx = ctx;
}
public CustomersDbAdapter open() throws SQLException
{
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long createCustomer(String customer, String name, String address1, String address2, String city) {
ContentValues initialValues = new ContentValues();
String searchValue = customer + " " +
name + " " +
address1 + " " +
city + " "
// state + " " + zipCode
;
initialValues.put(KEY_CUSTOMER, customer);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_ADDRESS1, address1);
initialValues.put(KEY_ADDRESS2, address2);
initialValues.put(KEY_CITY, city);
//initialValues.put(KEY_STATE, state);
//initialValues.put(KEY_ZIP, zipCode);
initialValues.put(KEY_SEARCH, searchValue);
return mDb.insert(FTS_VIRTUAL_TABLE, null, initialValues);
}
public Cursor searchCustomer(String inputText) throws SQLException {
Log.w(TAG, inputText);
String query = "SELECT docid as _id," +
KEY_CUSTOMER + "," +
KEY_NAME + "," +
"(" + KEY_ADDRESS1 + "||" +
"(case when " + KEY_ADDRESS2 + "> '' then '\n' || " + KEY_ADDRESS2 + " else '' end)) as " + KEY_ADDRESS +"," +
KEY_ADDRESS1 + "," +
KEY_ADDRESS2 + "," +
KEY_CITY +
//KEY_ZIP +
" from " + FTS_VIRTUAL_TABLE +
" where " + KEY_SEARCH + " MATCH '" + inputText + "';";
Log.w(TAG, query);
Cursor mCursor = mDb.rawQuery(query,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}