【发布时间】:2011-02-17 14:42:31
【问题描述】:
我有一个小型 Android 应用程序,目前我正在 android 中触发一条 sql 语句来获取数据库中特定 where 子句的行数。
以下是我的示例代码:
public boolean exists(Balloon balloon) {
if(balloon != null) {
Cursor c = null;
String count_query = "Select count(*) from balloons where _id = ?";
try {
c = getReadableDatabase().rawQuery(count_query, new String[] {balloon.getId()});
if (c.getCount() > 0)
return true;
} catch(SQLException e) {
Log.e("Running Count Query", e.getMessage());
} finally {
if(c!=null) {
try {
c.close();
} catch (SQLException e) {}
}
}
}
return false;
}
即使数据库表实际上是空的,此方法也会返回计数 1。我无法弄清楚;为什么会这样。在数据库中运行相同的查询让我的计数为 0。
我想知道是否可以记录或在日志中查看参数替换后最终在数据库上触发的所有 sql 查询;这样我就可以理解出了什么问题。
干杯
【问题讨论】:
-
你可以尝试改变 c = getReadableDatabase().rawQuery(count_query, new String[] {balloon.getId()});分成多行,每行做一件事,然后检查每一行?
标签: database android sqlite logging