Reputation: 45
I have a functioning database within my android app that returns one result queries. I am trying to return multiple results for a Wine keyword but every time I try the app crashes when i run it. I am fairly new to android coding so I could be missing something obvious.
The first .java is the search function. The second one is the query 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
}
});
}
}
Upvotes: 4
Views: 278
Reputation: 164099
Your query is syntactically wrong. The wildcard %
and the parameter winename
should be enclosed inside single quotes, but also it is recommended and safer to pass the parameter winename
in the 2nd argument of rawQuery()
.
Change to this:
c = db.rawQuery(
"select WineName from Wine where WineName LIKE '%' || ? || '%'",
new String[]{winename}
);
Upvotes: 1