如何检查,看看我的SQLite表中有数据? [英] How can i check to see if my sqlite table has data in it?

查看:191
本文介绍了如何检查,看看我的SQLite表中有数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改, 改变了code稍微基于下面的答案,但依然没有得到它的工作。我还添加了日志消息,告诉我,如果getCount将被返回> 0,这是,所以我supect财产以后可能是错的我的查询?或我使用了游标..

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 it was, so i supect somthing might be wrong with my query? or my use of the cursor..

我已经创建了一个表,我要检查其是否为空或不,如果它是空的,我想运行一些插入语句(存储在一个数组)。

I've created a table, and i want to check if its empty or not, if it's empty i want to run some insert statements (that are stored in an array).

下面是我的code,而我没有错误,当我拉.db文件出来,我可以看到,这是行不通的。你会如何​​处理这个问题?

Below is my code, while i have no errors, when i pull the .db file out i can see that it doesn't work. How would you approach this problem?

public void onCreate(SQLiteDatabase db) {
        Log.i("DB onCreate", "Creating the database...");//log message
        db.execSQL(createCATBUDTAB);
        db.execSQL(createTWOWEETAB);
        try{
            Cursor cur = db.rawQuery("SELECT COUNT(*) FROM CAT_BUD_TAB", null);
        if (cur.getCount() > 0){
            Log.i("DB getCount", " getcount greater than 0");//log message
            //do nothing everything's as it should be
        }
        else{//put in these insert statements contained in the array
            Log.i("DB getCount", " getcount less than 0, should read array");//log message
            for(int i=0; i<13; i++){
                db.execSQL(catInsertArray[i]);
            }
        }
        }catch(SQLiteException e){System.err.println("Exception @ rawQuery: " + e.getMessage());}
    }

抱歉,如果这是一个pretty的愚蠢的问题或办法,我是新来的这一切。任何答案多少AP preciated!

Sorry if this is a pretty stupid question or approach, i'm new to all this. Any answers much appreciated!

推荐答案

查询 SELECT COUNT(*)上现有的表应该的永远的返回null。如果没有行,在表中,则它应该返回一行包含零值。

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

部分。

要解决这个问题,留下您的查询的,是(你不想做选择列名仅仅是因为这将是不必要的,可能是有点低效率)。把它作为 SELECT COUNT(*),它总是会返回一行,并使用以下code(只在我的脑海测试,所以要小心):

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

这篇关于如何检查,看看我的SQLite表中有数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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