打开选项卡和数据库为空时 SQLite 错误 Android
我有四个选项卡,第一个选项卡在数据库中插入数据,其他两个选项卡负责使用光标显示数据。这是我的问题,但当我第一次启动应用程序时,如果我按 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只进行搜索
if(c.moveToFirst()) { ...搜索在这里...}
do the searching stuff only
if(c.moveToFirst()) { ...search comes here...}
我错过了最重要的元素,以下内容是我从中学到的,希望其他人也能学到。
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.