我想在我的应用程序中做一个简单的select语句。我要做的是返回与从列表视图接收的单词相关联的代码。当我运行代码时,我收到一个错误,指出列不存在。它说不存在的列实际上是传入语句的变量,而不是表中的任何列。任何帮助都会很棒
SQL声明
//gets recipe code given recipe name
public Cursor getCode(String name) throws SQLException
{
return db.rawQuery("select recipe_code from recipes where recipe_name = " + name ,null);
}
将变量传递给语句
TextView txtProduct =(TextView)findViewById(R.id.recipeTitle);
Intent i = getIntent();
// getting attached intent data
String string = i.getStringExtra("recipe");
// displaying selected product name
txtProduct.setText(string);
adapter.open();
Cursor code = adapter.getCode(string);
表创建
//Recipe create statement
private static final String CREATE_TABLE_RECIPES = "CREATE TABLE "
+ RECIPE_TABLE + "("
+ KEY_ID + " INTEGER AUTO INCREMENT,"
+ KEY_CODE + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
+ KEY_RECIPE_NAME + " TEXT" + ")";
答案 0 :(得分:1)
尝试使用这样的,
public Cursor getCode(String name) throws SQLException
{
return db.rawQuery("select recipe_code from recipes where recipe_name = " + "'"+name+"'" ,null);
}
将您的变量保持在单引号内。