您好,我在数据库处理程序类中都有此方法,该类所做的是从产品表中返回产品的ID。但是,我收到这个sqliteexception,我不知道为什么。
请指教谢谢。
private static final String TABLE_PRODUCT = "product"
private static final String KEY_PRODUCTNAME = "productname";
public String getProductId(String productName) {
String selectQuery = "SELECT productid FROM " + TABLE_PRODUCT+ " WHERE " +KEY_PRODUCTNAME +" = " + productName;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
String productid = cursor.toString();
cursor.close();
db.close();
return productid;
}
错误:
E/AndroidRuntime(1884): FATAL EXCEPTION: main
E/AndroidRuntime(1884): android.database.sqlite.SQLiteException: unrecognized token: "Bluedress34.50" (code 1): , while compiling: SELECT productid FROM product WHERE productname = "Bluedress34.50"
我的活动课:
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
String productname = pname.getText().toString();
String productQTY = pqty.getText().toString();
String productnameid = db.getUProductId(productname);
JSONObject json = userFunction.addSales(productnameid, productQty);
}
}
我的用户函数类:
public JSONObject addSales(productnameid, productQty){
// Building Parameters
List<NameValuePair> paramsfile = new ArrayList();
paramsfile.add(new BasicNameValuePair("productnameid", productnameid));
paramsfile.add(new BasicNameValuePair("productQty", productQty));
JSONObject jsonfileName = jsonParser.getJSONFromUrl(addFileURL, paramsfile);
Log.e("JSON", jsonfileName.toString());
return jsonfileName;
}
最佳答案
由于产品名称为字符串格式,因此在查询中使用'
,如下所示,
String selectQuery = "SELECT productid FROM " + TABLE_PRODUCT
+ " WHERE " + KEY_PRODUCTNAME +" ='" + productName +"'";