打开选项卡和数据库为空时 SQLite 错误 Android

发布于 2024-09-30 09:27:44 字数 1445 浏览 1 评论 0原文

我有四个选项卡,第一个选项卡在数据库中插入数据,其他两个选项卡负责使用光标显示数据。这是我的问题,但当我第一次启动应用程序时,如果我按 2 个选项卡之一,数据库上没有任何内容,则会出现以下错误:

ERROR/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: no such table

我的问题是,当按下两个选项卡之一时,如何避免出现此错误,我只想显示一个带有空列表的空选项卡。 这是发生错误的一段代码:

void populate() {
    array = new ArrayList<String>();
    mydb = openOrCreateDatabase("vivo_id.db",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);
    Cursor c = mydb.query("VIVOID", null, null, null, null, null, null);
    // start the managing cursor
    startManagingCursor(c);
    // move to first row
    c.moveToFirst();
    // continue searching until it is the last row in the column
    while (c.isAfterLast() == false) {
        sharable = c.getString(c.getColumnIndex("isCouponSharable"));
        coupon = c.getString(c.getColumnIndex("couponImageResult"));
        rawImage = c.getString(c.getColumnIndex("couponThumbnailResult"));
        keyword = c.getString(c.getColumnIndex("keyword"));
        histDesRes = c.getString(c.getColumnIndex("couponDescription"));
        history = keyword + " \n " + histDesRes + " " + "<@>" + ""
                + rawImage+""+"<@>"+""+coupon+""+"<@>"+""+sharable;
        array.add(history);

        c.moveToNext();
    }
    strings = array.toArray(new String[array.size()]);
    // close everything

    c.close();
    mydb.close();

}

任何帮助或指示将不胜感激,提前非常感谢。

I have four tabs which the first one inserts data on the database, the other 2 tabs take care of displaying the data using a cursor. here is my problem though when I first start the app and there is nothing on the database if I press one of the 2 tabs it gives me this error:

ERROR/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: no such table

my question is the following how can I avoid getting this error when pressing one of 2 tabs, I would like just to display an empty tab with an empty list.
Here is a piece of code where the error happens:

void populate() {
    array = new ArrayList<String>();
    mydb = openOrCreateDatabase("vivo_id.db",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);
    Cursor c = mydb.query("VIVOID", null, null, null, null, null, null);
    // start the managing cursor
    startManagingCursor(c);
    // move to first row
    c.moveToFirst();
    // continue searching until it is the last row in the column
    while (c.isAfterLast() == false) {
        sharable = c.getString(c.getColumnIndex("isCouponSharable"));
        coupon = c.getString(c.getColumnIndex("couponImageResult"));
        rawImage = c.getString(c.getColumnIndex("couponThumbnailResult"));
        keyword = c.getString(c.getColumnIndex("keyword"));
        histDesRes = c.getString(c.getColumnIndex("couponDescription"));
        history = keyword + " \n " + histDesRes + " " + "<@>" + ""
                + rawImage+""+"<@>"+""+coupon+""+"<@>"+""+sharable;
        array.add(history);

        c.moveToNext();
    }
    strings = array.toArray(new String[array.size()]);
    // close everything

    c.close();
    mydb.close();

}

Any help or pointers would be greatly appreciated, Thank you so much in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入画浅相思 2024-10-07 09:27:44

只进行搜索 if(c.moveToFirst()) { ...搜索在这里...}

do the searching stuff only if(c.moveToFirst()) { ...search comes here...}

夏至、离别 2024-10-07 09:27:44

我错过了最重要的元素,以下内容是我从中学到的,希望其他人也能学到。

myDbHelper.getReadableDatabase();
@函数的开头。

I was missing the most imprtant element which the following, I learned from it hopefully other people will as well.

myDbHelper.getReadableDatabase();
@ the beginning of the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文