我正在尝试实现一个使用原始查询但遇到麻烦的函数。我想做一个简单的select * from recipes where _id = 1
。我知道这一行存在,因为当我运行查询select * from recipes
时,我得到了预期的结果列表。但是当我使用where
子句时,我得不到任何结果。如果我如何在DB中的另一个表上执行where
子句,我会得到预期的结果。
任何人都可以告诉我哪里出错了吗?
表创建
//Recipe create statement
private static final String CREATE_TABLE_RECIPES = "CREATE TABLE "
+ RECIPE_TABLE + "("
+ KEY_ID + " INTEGER AUTO INCREMENT,"
+ KEY_CODE + " INTEGER AUTO INCREMENT PRIMARY KEY,"
+ KEY_RECIPE_NAME + " TEXT" + ")";
查询
return db.rawQuery("select * from recipes where _id = 1",null);
将结果放入列表
Cursor cursor = adapter.possibleRecipes();
//String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
String[] columns = new String[] {adapter.KEY_RECIPE_NAME, adapter.KEY_ID};
int[] to = new int[] {R.id.recipe_name, R.id.recipe_code};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.row1, cursor, columns, to, 0);
ListView recipeList = (ListView) findViewById(R.id.possibleRecipeList);
recipeList.setAdapter(myCursorAdapter);
答案 0 :(得分:1)
如果我没弄错的话,写的AUTOINCREMENT没有空格。在创建表时,我会编写INTEGER PRIMARY KEY AUTOINCREMENT。希望它有所帮助。
答案 1 :(得分:0)
什么是KEY_ID值?
另外,尝试替换
"select * from recipes where _id = 1"
使用
"select * from recipes where " + KEY_ID + " = 1"