gpt4 book ai didi

java - 需要更改 android studio 数据库中的 SQL 查询以返回多个结果

转载 作者:行者123 更新时间:2023-12-04 13:36:32 25 4
gpt4 key购买 nike

我的 android 应用程序中有一个功能正常的数据库,它返回一个结果查询。我正在尝试为 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应该用单引号括起来,但也建议传递参数winename更安全在 rawQuery() 的第二个参数中.
改成这样:

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

关于java - 需要更改 android studio 数据库中的 SQL 查询以返回多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61704571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com