Android SQLite检查表是否包含行 [英] Android SQLite checking if tables contain rows

查看:96
本文介绍了Android SQLite检查表是否包含行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在开发适用于Android的游戏,目前停留在主菜单中的加载保存游戏按钮上。
此按钮调用从数据库读取数据并将其写入资源类的方法,可以从该资源类访问该数据。

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;

如您所见,它在数据库表之一上开始查询并检查0列是否,这是应该在此游标中唯一的一个,为null。 ATM我无法在此调用中检查logcat结果,因为我似乎对此有一些问题,但是由于表为空,所以查询似乎引发了异常。

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.

是否想检查表中的行?

_ __ _ __ _ __ _ __ _ __ _ __ _ _EDIT_ _ __ _ __ _ __ _ __ _

____________________EDIT______________

注意:我检查了数据库,确定它为空

好的,我在表上使用了rawQuery,但是使用了count方法-statement产生了一个错误,所以我正在使用

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

我正在使用此方法来决定是否禁用loadGame-如下所示的按钮:

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.checkForTables());
    loadGame.setOnClickListener(new OnClickListener() {

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

        }
    });

因此,如果checkForTables的行计数为!= 0,它将返回true,因此启用Button,或者如果rowcount = 0则禁用它

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

有趣的是,尽管表为空,但checkForTables()返回true,因为getCount()似乎返回!= 0值-我只是不

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

推荐答案

从项表执行 select count(*)之类的查询。该查询将为您提供一个整数结果,其中包含该表中的行数。

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.

例如:

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



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



请注意,以下编辑是@PareshDudhat尝试的,但被审阅者拒绝。自从发布此答案以来,我一直没有跟上Android的步伐,但是一项非常简短的研究表明,该修改(至少是对 rawQuery()的调用方式有所改变,没有检查 moveToFirst(),但@ k2col的注释表明现在也需要它)。

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

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

这篇关于Android SQLite检查表是否包含行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆