开发者

Android SQLite checking if tables contain rows

So I'm working on a game for android and I'm currently stuck at the 'load savegame' button in the main menu. This button calls methods that read data from the database and write it into a resource class from which on this data will be accessed.

The problem is: I want to disable the load button if there are no rows in the tables, which means that no savegame exists.

To do this I used the following method:

public boolean checkForTables(){
    boolean hasTables;
    String[] column = new String[1];
    column[0] = "Position";
    Cursor cursor;
    cursor = db.query("itemtable", column, null, null, null, null, null);
    if(cursor.isNull(0) == true){
        hasTables=false;
    }else{
        hasTables=true;
    }
    return hasTables;

As you can see it starts a query on one of the database tables and checks if the 0-column, which is the only one that should be in this cursor, is null. ATM I can't check logcat results on this call because I seem to have some problems with it, but it seems that the query throws an exception because the table is empty.

Any idea to check the tables for rows?

____________________EDIT______________

NOTE: I checked the database and it sure is empty

Okay I used a rawQuery on the table but the approach with count-statement produced an error, so I'm using

public boolean checkForTables(){
        boolean hasTables;

        Cursor cursor = db.rawQuery("SELECT * FROM playertable", null);

        if(cursor.getCount() == 0){
            hasTables=false;
        if(cursor.getCount() > 0){
            hasTables=true;
        }

        cursor.close();
        return hasTables;
    }

I'm using this method to decide whether or not to disable the loadGame-button which looks like this:

loadGame = (ImageButton) findViewById(R.id.loadButton);
    loadGame.setEnabled(databaseAccess.开发者_Go百科checkForTables());
    loadGame.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            databaseAccess.loadPlayer();
            databaseAccess.loadItems();
            databaseAccess.dropTables();

        }
    });

So if checkForTables gets a rowcount of != 0 it will return true and therefore enable the Button, or disable it if rowcount = 0

Amusingly, although the tables are empty, checkForTables() returns true because getCount() seems to return a != 0 value - I just don't get it.


Perform a query such as select count(*) from itemtable. This query will yield you a single integer result, containing the number of rows in that table.

For example:

Cursor cursor = db.rawQuery("SELECT count(*) FROM itemtable");
if (cursor.getInt(0) > 0) ... // there are rows in the table

-------------------------------------------------------------------------------------

Please note that the following edit was attempted by @PareshDudhat but was rejected by reviewers. I have not kept up with Android since this answer was posted, but a very brief bit of research suggests the edit (at least the change to how rawQuery() is called, I didn't inspect the moveToFirst() but @k2col's comment suggests it is required now as well) has merit.

Cursor cursor = db.rawQuery("SELECT count(*) FROM itemtable",null);
cursor.moveToFirst();
if (cursor.getInt(0) > 0) ... // there are rows in the table


What mah says will work. Another approach you could use in your current function is:

hasTables = cursor.moveToFirst());

Note that this approach is probably only better to use if you plan on using the results of the query if hasTables is in fact true.

Also, don't forget to close your cursor when you are done with it!

EDIT

I don't know if this is your problem but in your edit you are querying for all items from the playerTable instead of the itemTable as you did in the pre-edit. Is that your problem?


cursor.getCount()

return the number of rows in database table.

and then try

Toast.makeText(this,""+cursor.getCount(),Toast.LENGTHLONG).show();

and it will give you no of rows in database table


The accepted answer put me on the right track, but didn't compile because rawQuery's method signature has changed and the cursor wasn't advanced to the first row before being read.

Here's my solution which includes error handling and closes the cursor:

public static boolean isEmpty() {

    boolean isEmpty;
    Cursor cursor = null;
    try {
        cursor = db.rawQuery("SELECT count(*) FROM itemtable", null);

        if (cursor.moveToFirst()) {
            isEmpty = cursor.getInt(0) == 0;
        } else {
            // Error handling here
        }
    } catch (SQLException e) {
        // Error handling here
    } finally {
        cursor.close();
    }
    return isEmpty;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜