返回变量中的记录数,Sqlite、Android、Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3589285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:33:04  来源:igfitidea点击:

Return the count of records in a variable, Sqlite, Android, Java

javaandroidsqlite

提问by Brian

My ultimate goal is to limit records that are able to be created so that I can have a trial version of my application. I'm thinking I can do this rather simply by returning an int variable within a sqlite count statement, and using a simple IF statement to determine if a new record should be created.

我的最终目标是限制能够创建的记录,以便我可以拥有我的应用程序的试用版。我想我可以通过在 sqlite count 语句中返回一个 int 变量并使用简单的 IF 语句来确定是否应该创建新记录来简单地做到这一点。

I call like so:

我这样称呼:

int jcount = 0;
            mDbHelper.countjournals(jcount);

Right now i'm trying to execute this

现在我正在尝试执行此操作

 public int countjournals(int jcount){
             mDb.execSQL(jcount + " = SELECT COUNT(*) FROM "+DATABASE_JOURNAL_TABLE);
            return jcount;
        }

error that i recieve:

我收到的错误:

08-27 22:42:32.417: ERROR/AndroidRuntime(3976): android.database.sqlite.SQLiteException: near "0": syntax error: 0 = SELECT COUNT(*) FROM journals

回答by Brian

Both the answers provided seemed to me like they should work, but i couldn't get them to. I've found a third solution that is working form me.

在我看来,提供的两个答案都应该有效,但我无法做到。我找到了第三种解决方案,它对我有用。

  public long countjournals() {

            return DatabaseUtils.queryNumEntries(mDb,DATABASE_JOURNAL_TABLE);

        }

回答by Aaron Saunders

public int countjournals() {
    SQLiteStatement dbJournalCountQuery;
    dbJournalCountQuery = mDb.compileStatement("select count(*) from" + DATABASE_JOURNAL_TABLE);
    return (int) dbJournalCountQuery.simpleQueryForLong();
}

回答by Konstantin Burov

Your query is incorrect, better way to do what you need is:

您的查询不正确,做您需要的更好的方法是:

    public int countjournals() {
        Cursor dataCount = mDb.rawQuery("select count(*) from" + DATABASE_JOURNAL_TABLE, null);
        dataCount.moveToFirst();
        int jcount = dataCount.getInt(0);
        dataCount.close();
        return jcount;
    }

Side note: Also note that you can't use primitive variables as references (and from you code it looks like you're trying to do so), also you can't pass in references to SQLite queries. Instead you just need to assign method (query) result to your variable.

旁注:另请注意,您不能使用原始变量作为引用(并且从您的代码看来您正在尝试这样做),您也不能传递对 SQLite 查询的引用。相反,您只需要将方法(查询)结果分配给您的变量。

回答by Skomo

Just insert a SPACE after froman it start working.....

只需在from它开始工作后插入一个空格.....

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