搜索SQLite数据库

时间:2017-07-22 19:48:24

标签: java android sqlite search

[不重复]这个问题是关于导致我的应用程序崩溃的搜索功能,只需要知道我的搜索方法是否有问题。

我有一个带文本框和按钮的简单搜索类,但只想完成功能,但是一旦点击搜索按钮,应用程序就会崩溃。 它意味着输出3列,在文本框中搜索名称。

我尝试获取完全有效的所有数据,这些数据位于不同的片段中,并输入了我需要输入的确切值但是 我不确定我在这里做错了什么。

在Database类中搜索方法;

    public String searchData(String name) {
    SQLiteDatabase db = this.getWritableDatabase();
    //gonna get calories and quantity only
    String[] columns = {COL_1, COL_2, COL_3};
    //searching for name
    Cursor cursor = db.query(TABLE_NAME, columns, COL_1+" = '"+name+"'", null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext()) {
        //returning name quantity and calories
        int columnName = cursor.getColumnIndex(COL_1);
        int columnQuantity = cursor.getColumnIndex(COL_2);
        int columnCalories = cursor.getColumnIndex(COL_3);

        String foodName = cursor.getString(columnName);
        String quantity = cursor.getString(columnQuantity);
        String calories = cursor.getString(columnCalories);

        buffer.append("Name: " + foodName + "\n" + "Serving size: " + quantity + "\n" + "Calories: " + calories);

    }
    return buffer.toString();

这是我的片段类,用户输入并点击搜索按钮来搜索数据库中的数据;

 public void Search(View view) {
    searchButtonVariable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String getNameText = searchTextVariable.getText().toString();
            String output = db.searchData(getNameText);
            showMessage("Data", output);
        }
    });

}

这是我在logcat中得到的:

07-22 20:50:05.531 2265-5706/? W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream
07-22 20:50:05.532 2265-2607/? I/MicroRecognitionRunner: Stopping hotword detection.
07-22 20:50:05.535 2265-5706/? W/ErrorProcessor: onFatalError, processing error from engine(4)
                                                 com.google.android.apps.gsa.shared.speech.b.g: Error reading from input stream
                                                     at com.google.android.apps.gsa.staticplugins.recognizer.j.a.a(SourceFile:28)
                                                     at com.google.android.apps.gsa.staticplugins.recognizer.j.b.run(SourceFile:15)
                                                     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:14)
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4)
                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                     at java.lang.Thread.run(Thread.java:764)
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.ai.run(SourceFile:6)
                                                  Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
                                                     at com.google.android.apps.gsa.speech.audio.Tee.f(SourceFile:103)
                                                     at com.google.android.apps.gsa.speech.audio.au.read(SourceFile:2)
                                                     at java.io.InputStream.read(InputStream.java:101)
                                                     at com.google.android.apps.gsa.speech.audio.ao.run(SourceFile:18)
                                                     at com.google.android.apps.gsa.speech.audio.an.run(SourceFile:2)
                                                     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457) 
                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:14) 
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4) 
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.bl.run(SourceFile:4) 
                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) 
                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) 
                                                     at java.lang.Thread.run(Thread.java:764) 
                                                     at com.google.android.apps.gsa.shared.util.concurrent.a.ai.run(SourceFile:6) 

1 个答案:

答案 0 :(得分:0)

使用此查询

Cursor c = db.rawQuery(
            "select * from table_name where column_name = ?",
            new String[] { name });