2012-04-16 64 views
0

當我嘗試通過使用'Washing Machine'作爲搜索字符串來搜索我的數據庫以查找'Washing Machine'的數據庫條目時,出現一個錯誤消息:語法在數據庫搜索條目時出錯

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 

,更具體地:

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) 

這裏是數據庫的搜索查詢代碼:

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

而這正是該方法創建原始類:

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

任何人都不會有爲什麼這個錯誤信息出現的任何想法?由於

編輯:這裏是代碼的錯誤指向太行(行122):

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

您能顯示導致錯誤的實際sql命令字符串嗎? – 2012-04-16 21:54:18

+0

我已經把它放在原帖的末尾,謝謝 – Sketzii 2012-04-16 21:57:14

+0

我希望你已經做了一些調試,並且可以將原始字符串值拉出來讓我們看看:) – 2012-04-16 22:01:44

回答

1

提供的錯誤消息,好像你沒有引號表示爲一個字符串appliance_name價值比較。它應該是

WHERE appliance_name = 'Washing Machine' 

我不熟悉的API,但你可以嘗試改變線122

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

是的,它現在正在工作!非常感謝,這是它的單引號標記:) – Sketzii 2012-04-16 22:10:56

1

你需要附上您搜索在單引號的字符串(在您的SQL查詢) 。