搜索在"数据库浏览器中为SQLite创建的数据库"

时间:2015-07-26 21:36:58

标签: android database sqlite search

对我希望我的应用做什么的一点描述: 我希望活动中的搜索小部件(不是操作栏),我希望能够搜索我将使用数据库浏览器创建的数据库,我还希望搜索结果显示在同一个活动中(如果是可能的)。

我的数据库将包含3列:项目,类别和说明。 (我还需要一个key_id吗?)。

我一直在使用http://developer.android.com/guide/topics/search/search-dialog.html作为指导。我一路走到"搜索您的数据"部分,我不知道如何进行。我已经尝试使用谷歌搜索如何做到这一点,我能够偶然发现http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/向我展示了如何在Android应用程序中访问数据库。我还查看过" Searchable词典"在android链接中引用的示例。

这是我第一次做这么大的事情,所以我慢慢开始感到不知所措。我在网上找到的很多教程都展示了如何在应用程序运行时创建/更新/添加/删除数据库和条目,但如果我使用数据库浏览器,这是必要的吗?很多代码都有简单的东西,如声明列,但我不确定在我的情况下是否需要这些。如果任何人都可以提供关于合并搜索功能(一个符合我上面的需求)或指向教程的链接的一般概念的概述,那将不胜感激。现在,我有一个搜索活动,一个dbHelper类,我将需要游标,据我所知,一个将显示结果的类?除非我的搜索活动中的内容也不合适。

SEARCH:

公共类搜索扩展了ActionBarActivity {

private TextView mTextView;
private ListView mListView;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void toMainMenu(View view) {
    Intent i=new Intent(Search.this,MainMenu.class);
    startActivity(i);
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        doSearchQuery(query);
    }
}

public void doSearchQuery(String query){
    //DataBaseHelper myDbHelper = new DataBaseHelper();
    DataBaseHelper myDbHelper = new DataBaseHelper(this);

    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
}

}

我不确定在哪里放置createDataBase和openDataBase。它们应该在doSearchQuery之外吗?

dbHELPER:

公共类DataBaseHelper扩展了SQLiteOpenHelper {

//The Android's default system path of your application database.
public static String DB_PATH = "/data/data/com.example.gus.howtorecycle/databases/";
public static String DB_NAME = "RecycleDB.db";

public static int DB_VERSION = 1;
public static String TABLE_NAME = "Recycle_Table";
public static String COL_1 = "Item";
public static String COL_2 = "Category";
public static String COL_3 = "Description";

private SQLiteDatabase myDataBase;

private final Context myContext;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 */
public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method and empty database will be created into the default system path
        //of your application so we are gonna be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }catch(SQLiteException e){

        //database does't exist yet.

    }

    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{

    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

    if(myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.

}

只是尝试总结一下。我有它,所以它收到一个搜索查询,我有一个数据库,我需要的是在Android中创建该数据库,使用光标搜索它(doSearchQuery),然后才能显示结果。

所以,如果我失去了任何人,我的头脑会有点混乱。如果有人需要澄清,我会监控并回答问题!

0 个答案:

没有答案