Syntax Error when searching Database for an entry

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);

Answers


Given the error message, it seems like you don't have quotes denoting a string for the appliance_name value comparison. It should be

WHERE appliance_name = 'Washing Machine'

I'm not familiar with the api, but you can try changing line 122 to

Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "='" + spinnerChoice + "'", null, null, null, null);

You need to enclose you search string in single quotes (in your SQL query).


Need Your Help

Exporting specific pattern of string using split method in a most efficient way

java string methods pattern-matching

I want to export pattern of bit stream in a String varilable. Assume our bit stream is something like bitStream="111000001010000100001111". I am looking for a Java code to save this bit stream in a