when I try to search my database for a specific entry by using ‘Washing Machine’ as the search string to try and find the Database entry for ‘Washing Machine’, an error appears saying:
04-16 21:43:28.951: E/AndroidRuntime(545): FATAL EXCEPTION: main
04-16 21:43:28.951: E/AndroidRuntime(545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lukeorpin.theappliancekeeper/com.lukeorpin.theappliancekeeper.EntryStatis tics}: android.database.sqlite.SQLiteException: near "Machine": syntax error: , while compiling: SELECT _id, appliance_name, appliance_wattage, energy_rates FROM ApplianceDetails WHERE appliance_name=Washing Machine
and more specifically to:
at com.lukeorpin.theappliancekeeper.Database.getWattage(Database.java:122)
04-16 21:43:28.951: E/AndroidRuntime(545): at com.lukeorpin.theappliancekeeper.EntryStatistics.onCreate(EntryStatistics.java:47)
Here is the code for the search query for the Database:
public String getWattage(String spinnerChoice) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_WATTAGE, KEY_ENERGY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + spinnerChoice, null, null, null, null);
if (c != null){
c.moveToFirst();
String wattage = c.getString(2);
return wattage;
}
return null;
}
public String getEnergyRate(String spinnerChoice) {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_WATTAGE, KEY_ENERGY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + spinnerChoice, null, null, null, null);
if (c != null){
c.moveToFirst();
String energyRate = c.getString(3);
return energyRate;
}
return null;
}
and this is the original class where the method was created:
final String spinnerChoice = getIntent().getStringExtra("Name");
if(spinnerChoice==null){
return;
}
Database data = new Database(this);
data.open();
String returnedWattage = data.getWattage(spinnerChoice);
String returnedEnergyRate = data.getEnergyRate(spinnerChoice);
data.close();
Does anyone have any ideas why this error message is appearing? Thanks
EDIT: Here is the line of code that the error is pointing too (line 122):
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + spinnerChoice, null, null, null, null);
Given the error message, it seems like you don’t have quotes denoting a string for the
appliance_namevalue comparison. It should beI’m not familiar with the api, but you can try changing line 122 to