5

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.

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

2
  • i think you are going to need to provide additional information to get an acceptable answer Commented Aug 28, 2010 at 6:15
  • I chose my answer because it was the one that worked for me. Although it looks, to me at least, that they should all work. Commented Aug 30, 2010 at 15:38

4 Answers 4

14

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);

        }
Sign up to request clarification or add additional context in comments.

1 Comment

Very awesome, I missed class DatabaseUtils
6
public int countjournals() {
    SQLiteStatement dbJournalCountQuery;
    dbJournalCountQuery = mDb.compileStatement("select count(*) from" + DATABASE_JOURNAL_TABLE);
    return (int) dbJournalCountQuery.simpleQueryForLong();
}

2 Comments

what should dbJournalCountQuery be defined as?
This seems to work as well. But I'm still only getting 1 as the result when there are 4
3

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.

4 Comments

no errors, but only gives me a count of 1 when there are more.
That can be for two reasons: 1 - there is really only one record, 2 - probably there are more, but only 1 was already committed to the db. The code snippet is almost 100% copy of my production, so I'm totally sure it is correct.
To me it looks like it should work. What do you mean by already committed?
Even if there are no records I am still getting a value of 1
1

Just insert a SPACE after from an it start working.....

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.