英文:
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)
这些表是:
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): , while compiling: SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description FROM QUANTITY AS A JOIN INGREDIENTS AS B ON A.ingredient = B.ingredient_name
英文:
I'm struggling on an sql query I have:
"SELECT A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description " +
"FROM TABLE_QUANTITY JOIN TABLE_INGREDIENT ON A.ingredient = B.ingredient_name"
This is telling me:
> no such table: TABLE_QUANTITY (code 1 SQLITE_ERROR)
The tables are:
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 + "))";
Update
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();
}
}
Made some chnages based on suggestions, thankyou. However still getting
Caused by: android.database.sqlite.SQLiteException: no such table: QUANTITY (code 1 SQLITE_ERROR): , while compiling: SELECT + A.ingredient_quantity, B.measurement_name, B.ingredient_name, B.description FROM QUANTITY AS A JOIN INGREDIENTS AS B ON A.ingredient = B.ingredient_name
答案1
得分: 0
`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
在两个表的 `CREATE` 语句中都是错误的。<br/>
移除逗号,使其变为:
INTEGER PRIMARY KEY AUTOINCREMENT
您可能需要从设备中卸载该应用,以便删除数据库,然后重新运行应用以重新创建数据库和表。
英文:
TABLE_QUANTITY
and TABLE_INGREDIENT
are string variables that you must concatenate inside the SQL statement and set the aliases A and B also:
"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"
What is also wrong is this:
INTEGER PRIMARY KEY, AUTO INCREMENT
in both CREATE
statements of the 2 tables.<br/>
Remove the comma so it is:
INTEGER PRIMARY KEY AUTOINCREMENT
You may have to uninstall the app from the device so the db is deleted and rerun to recreate the db and the tables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论