Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 914
Brian Avatar asked Aug 28 '10 03:08

Brian


2 Answers

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

        }
like image 56
Brian Avatar answered Nov 15 '22 05:11

Brian


public int countjournals() {
    SQLiteStatement dbJournalCountQuery;
    dbJournalCountQuery = mDb.compileStatement("select count(*) from" + DATABASE_JOURNAL_TABLE);
    return (int) dbJournalCountQuery.simpleQueryForLong();
}
like image 42
Aaron Saunders Avatar answered Nov 15 '22 07:11

Aaron Saunders