需要将 android studio 数据库中的 SQL 查询更改为 return 多个结果

Need to change SQL query in android studio database to return multiple results

我的 android 应用程序中有一个正常运行的数据库,return 有一个结果查询。我正在尝试 return 一个 Wine 关键字的多个结果,但每次我尝试 运行 时应用程序都会崩溃。我对 android 编码还很陌生,所以我可能会遗漏一些明显的东西。

第一个.java是搜索功能。第二个是查询 activity.

package com.example.winedatabase;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseAccess {
    private SQLiteOpenHelper openHelper;
    private SQLiteDatabase db;
    private static DatabaseAccess instance;
    Cursor c = null;

    //private constructor so that object creation from outside is avoided
    private DatabaseAccess(Context context){
        this.openHelper = new DatabaseOpenHelper(context);

    }

    //to return the single instance of database
    public static DatabaseAccess getInstance(Context context){
        if(instance == null){
            instance= new DatabaseAccess(context);

        }
        return instance;
    }

    //to open the database
    public void open(){
        this.db = openHelper.getWritableDatabase();
    }
    //closing the database connection
    public void close(){
        if (db != null) {
            this.db.close();
        }
    }
    //method to query and return the result from the database
    public String getKeyword(String winename){
        c = db.rawQuery(" select WineName from Wine where WineName LIKE %'"+winename+"'%", new String[]{});
        StringBuffer buffer = new StringBuffer();
        while(c.moveToNext()){
            String description = c.getString(0);
            buffer.append(""+description);
        }
        return buffer.toString();
    }

}

package com.example.winedatabase;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class KeywordSearch extends AppCompatActivity {

    public EditText name;
    public Button query_button;
    public TextView result_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_keyword_search);

        name = findViewById(R.id.name);
        query_button = findViewById(R.id.query_button);
        result_address = findViewById(R.id.result);

        //set onClickListnener to query button

        query_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //create the instance of database access class and open database connection

                DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
                databaseAccess.open();

                //get string value of edittext

                String n = name.getText().toString();
                String description = databaseAccess.getKeyword(n);  //used the get address method to get address

                //set text to result field
                result_address.setText(description);
                databaseAccess.close();
                //database connection closed

            }
        });
    }
}

您的查询在语法上有误。通配符 % 和参数 winename 应该用单引号括起来,但也建议在 rawQuery() 的第二个参数中传递参数 winename 更安全。
更改为:

c = db.rawQuery(
    "select WineName from Wine where WineName LIKE '%' || ? || '%'", 
    new String[]{winename}
);