2016-01-22 105 views
-1

我想在我的应用程序中做一个简单的选择语句。我试图做的是返回一个代码,该代码与从列表视图中收到的单词相关联。当我运行代码时,出现错误提示列不存在。它表示不存在的列实际上是我传递给语句的变量,而不是表中的任何列。任何帮助将是巨大的Android没有这样的变量名列

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

你在哪里创建代码表?你可以发布吗? –

回答

0

尝试使用这样的,

public Cursor getCode(String name) throws SQLException 
{ 
    return db.rawQuery("select recipe_code from recipes where recipe_name = " + "'"+name+"'" ,null); 
} 

保持单引号内的变量。

+0

谢谢,完全忘记了单引号 – Hayes121

+0

很高兴帮助你。 – Ajinkya

+0

只是另一个问题,如果我想在另一个语句中使用游标中返回的值,我该如何将值提取到一个变量? – Hayes121