Android 没有这样的 table 代码 1 SQLITE_ERROR
Android No such table code 1 SQLITE_ERROR
我正在为一个 sql 查询而苦苦挣扎 我有:
"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM TABLE_QUANTITY JOIN TABLE_INGREDIENT ON A.ingredient = B.ingredient_name"
这告诉我:
no such table: TABLE_QUANTITY (code 1 SQLITE_ERROR)
table是:
private static final String CREATE_TABLE_QUANTITY = "CREATE TABLE " + TABLE_QUANTITY + "(" + COL_ID +
" INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_QUANTITY + " NUMERIC, " +
COL_RECIPE + " TEXT," + COL_INGREDIENT + " TEXT, FOREIGN KEY (" + COL_RECIPE + ") " +
"REFERENCES " + TABLE_RECIPE + "(" + COL_RECIPE_NAME + "), FOREIGN KEY (" + COL_INGREDIENT + ") " +
"REFERENCES " + TABLE_INGREDIENTS + "(" + COL_INGREDIENT_NAME + "))";
private static final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE " + TABLE_INGREDIENTS + "("
+ COL_ID + " INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_NAME + " TEXT,"
+ COL_DESCRIPTION + " TEXT," + COL_MEASUREMENT + " TEXT," + COL_INGREDIENT_TYPE + " TEXT, " +
"FOREIGN KEY (" + COL_MEASUREMENT + ") REFERENCES " + TABLE_MEASUREMENT + "(" + COL_MEASUREMENT_NAME + "), " +
"FOREIGN KEY (" + COL_INGREDIENT_TYPE + ") REFERENCES " + TABLE_INGREDIENT_TYPE + "(" + COL_TYPE_NAME + "))";
更新
public void loadRecipe() {
itemRecipe.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name";
String selectQuery = "";
selectQuery = selectQuery + RECIPE_SEARCH;
c = db.rawQuery(selectQuery, new String[]{"%" + search_name + "%"});
if (c.moveToFirst()) {
do {
RecipeList recipeList = new RecipeList();
recipeList.setId(c.getInt(c.getColumnIndex("COL_ID")));
recipeList.setIngredient_amount(c.getString(c.getColumnIndex("COL_INGREDIENT_QUANTITY")));
recipeList.setMeasurement_name(c.getString(c.getColumnIndex("COL_MEASUREMENT_NAME")));
recipeList.setIngredient_name(c.getString(c.getColumnIndex("COL_INGREDIENT_NAME")));
recipeList.setDescription(c.getString(c.getColumnIndex("COL_DESCRIPTION")));
itemRecipe.add(recipeList);
} while (c.moveToNext());
c.close();
}
}
根据建议做了一些修改,谢谢。然而还是越来越
Caused by: android.database.sqlite.SQLiteException: no such table: QUANTITY (code 1 SQLITE_ERROR): , 编译时: SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description 从数量作为加入成分作为 B 在 A.ingredient = B.ingredient_name
TABLE_QUANTITY
和 TABLE_INGREDIENT
是字符串变量,您必须在 SQL 语句中连接它们并同时设置别名 A 和 B:
"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + TABLE_QUANTITY + "AS A JOIN " + TABLE_INGREDIENT +
" AS B ON A.ingredient = B.ingredient_name"
同样错误的是:
INTEGER PRIMARY KEY, AUTO INCREMENT
在 2 个表的两个 CREATE
语句中。
去掉逗号,变成:
INTEGER PRIMARY KEY AUTOINCREMENT
您可能必须从设备上卸载该应用程序,以便删除数据库并重新运行以重新创建数据库和表。
我正在为一个 sql 查询而苦苦挣扎 我有:
"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM TABLE_QUANTITY JOIN TABLE_INGREDIENT ON A.ingredient = B.ingredient_name"
这告诉我:
no such table: TABLE_QUANTITY (code 1 SQLITE_ERROR)
table是:
private static final String CREATE_TABLE_QUANTITY = "CREATE TABLE " + TABLE_QUANTITY + "(" + COL_ID +
" INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_QUANTITY + " NUMERIC, " +
COL_RECIPE + " TEXT," + COL_INGREDIENT + " TEXT, FOREIGN KEY (" + COL_RECIPE + ") " +
"REFERENCES " + TABLE_RECIPE + "(" + COL_RECIPE_NAME + "), FOREIGN KEY (" + COL_INGREDIENT + ") " +
"REFERENCES " + TABLE_INGREDIENTS + "(" + COL_INGREDIENT_NAME + "))";
private static final String CREATE_TABLE_INGREDIENTS = "CREATE TABLE " + TABLE_INGREDIENTS + "("
+ COL_ID + " INTEGER PRIMARY KEY, AUTO INCREMENT," + COL_INGREDIENT_NAME + " TEXT,"
+ COL_DESCRIPTION + " TEXT," + COL_MEASUREMENT + " TEXT," + COL_INGREDIENT_TYPE + " TEXT, " +
"FOREIGN KEY (" + COL_MEASUREMENT + ") REFERENCES " + TABLE_MEASUREMENT + "(" + COL_MEASUREMENT_NAME + "), " +
"FOREIGN KEY (" + COL_INGREDIENT_TYPE + ") REFERENCES " + TABLE_INGREDIENT_TYPE + "(" + COL_TYPE_NAME + "))";
更新
public void loadRecipe() {
itemRecipe.clear();
db = (new DatabaseManager(this).getWritableDatabase());
String RECIPE_SEARCH = " SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + DatabaseManager.TABLE_QUANTITY + " AS A JOIN " + DatabaseManager.TABLE_INGREDIENTS +
" AS B ON A.ingredient = B.ingredient_name";
String selectQuery = "";
selectQuery = selectQuery + RECIPE_SEARCH;
c = db.rawQuery(selectQuery, new String[]{"%" + search_name + "%"});
if (c.moveToFirst()) {
do {
RecipeList recipeList = new RecipeList();
recipeList.setId(c.getInt(c.getColumnIndex("COL_ID")));
recipeList.setIngredient_amount(c.getString(c.getColumnIndex("COL_INGREDIENT_QUANTITY")));
recipeList.setMeasurement_name(c.getString(c.getColumnIndex("COL_MEASUREMENT_NAME")));
recipeList.setIngredient_name(c.getString(c.getColumnIndex("COL_INGREDIENT_NAME")));
recipeList.setDescription(c.getString(c.getColumnIndex("COL_DESCRIPTION")));
itemRecipe.add(recipeList);
} while (c.moveToNext());
c.close();
}
}
根据建议做了一些修改,谢谢。然而还是越来越 Caused by: android.database.sqlite.SQLiteException: no such table: QUANTITY (code 1 SQLITE_ERROR): , 编译时: SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description 从数量作为加入成分作为 B 在 A.ingredient = B.ingredient_name
TABLE_QUANTITY
和 TABLE_INGREDIENT
是字符串变量,您必须在 SQL 语句中连接它们并同时设置别名 A 和 B:
"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM " + TABLE_QUANTITY + "AS A JOIN " + TABLE_INGREDIENT +
" AS B ON A.ingredient = B.ingredient_name"
同样错误的是:
INTEGER PRIMARY KEY, AUTO INCREMENT
在 2 个表的两个 CREATE
语句中。
去掉逗号,变成:
INTEGER PRIMARY KEY AUTOINCREMENT
您可能必须从设备上卸载该应用程序,以便删除数据库并重新运行以重新创建数据库和表。