我在sql中有这个查询
UPDATE Flavor a
SET count = (SELECT COUNT(*)
FROM recipes c
WHERE c.flv1name = a.name)
返回名为flv1name的列中名为recipes的表中所有条目的计数。然后它会在名为count的列中更新第二个名为flavors的表,其中包含来自flv1name的条目总数,如下所示。
Table Flavors
_id name count
1 Apple 2
2 Berry 1
3 Cherry 4
问题是,如何在我的Android应用程序中的数据库助手中编写此语句?
这是我到目前为止所拥有的
public Cursor syncflavorcount() {
mDb = mDbHelper.getReadableDatabase();
String buildSQL = "UPDATE " + DATABASE_TABLE2 + " a SET count = (SELECT COUNT(*) FROM " + DATABASE_TABLE + " c WHERE c.flv1name = a.name) ";
return mDb.rawQuery(buildSQL, null);
}
答案 0 :(得分:0)
mDb.update("UPDATE " + DATABASE_TABLE2 + " a SET count = (SELECT COUNT(*) FROM " + DATABASE_TABLE + " c WHERE c.flv1name = a.name)")
答案 1 :(得分:0)
为了在Android应用程序数据适配器中工作,必须删除别名。
从此
mDb.update("UPDATE " + DATABASE_TABLE2 + " a SET count = (SELECT COUNT(*) FROM " + DATABASE_TABLE + " c WHERE c.flv1name = a.name)")
到此
mDb.update("UPDATE " + DATABASE_TABLE2 + " SET count = (SELECT COUNT(*) FROM " + DATABASE_TABLE + " WHERE " +DATABASE_TABLE2+ ".flv1name = " +DATABASE_TABLE+".name)");