【问题标题】:Sqlite error when opening a TAB and when the Database is empty Android打开 TAB 和数据库为空 Android 时出现 Sqlite 错误
【发布时间】:2010-11-03 07:13:55
【问题描述】:

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

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

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

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();

}

任何帮助或指点将不胜感激,在此先感谢您。

【问题讨论】:

    标签: android sqlite listview tabs


    【解决方案1】:

    只搜索if(c.moveToFirst()) { ...search comes here...}

    【讨论】:

    • 能否请您详细说明一下答案,非常感谢。
    【解决方案2】:

    我遗漏了以下最重要的元素,我从中学到了希望其他人也能从中学到的东西。

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

    【讨论】:

      猜你喜欢
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 2011-05-01
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      相关资源
      最近更新 更多