SQLite 计数示例

发布于 2024-11-03 21:41:20 字数 185 浏览 0 评论 0原文

我在 Android 中使用 SQLite。 我有查询、执行的查询以及如何从光标打印计数。

Cursor dataCount = mDb.rawQuery("select count(*) from " + DATABASE_JOURNAL_TABLE, null);

我的表中没有记录。

I am using SQLite in Android.
I have the query, query executed and how to print count from cursor.

Cursor dataCount = mDb.rawQuery("select count(*) from " + DATABASE_JOURNAL_TABLE, null);

I have no record in table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

没有心的人 2024-11-10 21:41:20

你已经有了正确的方法。

Cursor cursor = database.rawQuery("select count(*) from " + DATABASE_JOURNAL_TABLE, null);

// ensure there is at least one row and one column
if (cursor.getCount() > 0 && cursor.getColumnCount() > 0) {
    cursor.close();
    return cursor.getInt(0);
} else {
    cursor.close();
    return 0;
}

您必须检查是否至少有 1 行和 1 列,如果您提供的表尚不存在,则将没有列可供访问,cursor.getInt(0) 将抛出异常。

来源:https://github.com/samkirton/SQLKing

You already have the correct approach.

Cursor cursor = database.rawQuery("select count(*) from " + DATABASE_JOURNAL_TABLE, null);

// ensure there is at least one row and one column
if (cursor.getCount() > 0 && cursor.getColumnCount() > 0) {
    cursor.close();
    return cursor.getInt(0);
} else {
    cursor.close();
    return 0;
}

You must check that there is at least 1 row and 1 column, if you provide a table that does not yet exist there will be no column to access and cursor.getInt(0) will throw an exception.

source: https://github.com/samkirton/SQLKing

能怎样 2024-11-10 21:41:20

可能是 getInt(index)

cursor.getInt(1); // this is for example, you have to adjust index in your code

可以光标有一个内置函数 getCount()返回行号,所以也可以这样做:

// assuming your table has `id` column as primary key or unique key.
Cursor dataCount = mDb.rawQuery("select id from " + DATABASE_JOURNAL_TABLE, null);
dataCount.getCount();

请参阅 android devloper's doc for Cursor了解更多信息。

May be by getInt(index) as

cursor.getInt(1); // this is for example, you have to adjust index in your code

Also cursor has a built in function getCount() to return row number so can also do like this:

// assuming your table has `id` column as primary key or unique key.
Cursor dataCount = mDb.rawQuery("select id from " + DATABASE_JOURNAL_TABLE, null);
dataCount.getCount();

See android devloper's doc for Cursor for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文