How can i check to see if my sqlite table has data in it?

后端 未结 13 1519
情话喂你
情话喂你 2020-12-01 13:14

EDIT, Changed the code slightly based on answers below, but still haven\'t got it working. I also added a log message to tell me if getCount was returning > 0, and i

相关标签:
13条回答
  • 2020-12-01 13:38

    My app was crashing trying the above codes, so I did this and now it's working perfectly!

    public boolean checkIfEmpty()
    {
        Cursor cursor = getDatabase().query(DatabaseHelper.Products.TABLE,
                DatabaseHelper.Products.COLUMNS, null, null, null, null, null);
    
        if (cursor != null)
        {
            try
            {
                //if it is empty, returns true.
                cursor.moveToFirst();
                if (cursor.getInt(0) == 0)
                    return true;
                else
                    return false;
            }
    
            //this error usually occurs when it is empty. So i return true as well. :)
            catch(CursorIndexOutOfBoundsException e)
            {
                return true;
            }
    
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-12-01 13:39

    You can check it manually :

    1. Go to DDMS in File Explorer. /data/data/(your application package)/databases Here you'll get your database file. And if you want to see inserted values in table.

    2. Copy adb, aapt, AdbWinApi.dll and AdbWinUsbApi.dll files from platform_tools to tools folder.

    3. Then Open Command Prompt and set your path to "Tools" directory where your android setup exist. It look like as :- (Local Drive):\Android\android-sdk-windows\tools

    4. After set path you'll write command "adb shell" without quotes. E:\Developers\Android\android-sdk-windows\tools> adb shell

    5. Press Enter and write path of DDMS told above:- # sqlite3 /data/data/(Your Application Package)/databases/name of database

    6. Press Enter sqlite>.tables

    7. Press Enter And you'll get all table's name existing in that database.

    sqlite> Select * from table name
    

    All data exists in that table will show.

    For any further query feel free to comment.

    0 讨论(0)
  • 2020-12-01 13:43

    Another alternative to the one already mentioned would be to use the function queryNumEntries from de class DatabaseUtils.

    An example may be as follows:

    public boolean checkEmpty(SQLiteDatabase db, String table){
            return DatabaseUtils.queryNumEntries(db, table) == 0;
    }
    
    0 讨论(0)
  • 2020-12-01 13:45

    The query SELECT COUNT(*) on an existing table should never return null. If there are no rows in the table, it should return one row containing the value zero.

    Conversely, a row with a non-zero value indicates that it's not empty.

    In both cases, one row should be returned, meaning that it will always go through the

    //do nothing everything's as it should be
    

    section.

    To fix it, leave your query as-is (you don't want to do select column_name simply because that would be unnecessary and possibly a little inefficient). Leave it as select count(*), which will always return one row, and use the following code (tested only in my head so be careful):

    Cursor cur = db.rawQuery("SELECT COUNT(*) FROM CAT_BUD_TAB", null);
    if (cur != null) {
        cur.moveToFirst();                       // Always one row returned.
        if (cur.getInt (0) == 0) {               // Zero count means empty table.
            for (int i = 0; i < 13; i++) {
                db.execSQL (catInsertArray[i]);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-01 13:48

    The rawQuery returns a Cursor object which is positioned before the first entry (See more info here) SELECT COUNT(*) will always return a result (considering the table exists)

    So I would do:

    if (cur != null){
        cur.moveToFirst();
        if (cur.getInt(0) == 0) {
          // Empty 
        }
    
    }
    
    0 讨论(0)
  • 2020-12-01 13:48

    My way of checking was something like this:

    Cursor cursor = db.query(DbHelper.DB_NAME,DbHelper.DB_C_ID_ONLY,null,null,null,null,null);
    if(cursor.isAfterLast())
    

    I get the _id entries which in my case are auto-increment. If the DB is empty, isAfterLast for sure returns true.

    0 讨论(0)
提交回复
热议问题