【问题标题】:can't insert into sqlite database, error: Table doen't exist无法插入 sqlite 数据库,错误:表不存在
【发布时间】:2015-04-12 13:44:41
【问题描述】:

在过去的 3 天里,我一直在尝试解决看似简单的问题,我正在尝试将记录从活动中插入到 sqlite 数据库中,我已经搜索了这个问题而没有看。您可以在下面找到我的活动代码和 DBHelper 代码。 dbhelper 代码:

  public Long insertResult(String QUEST_ID,String test1,String test2) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues initialValues = new ContentValues();
    initialValues.put("Question", QUEST_ID);
    initialValues.put("Rights", test1);
    initialValues.put("Wrongs", test2);
    Log.d("Result:", "Rights " + initialValues.get("Rights"));
    return  db.insertOrThrow("Results", null, initialValues);
}

from the mainactivity
 long ins = myDbHelper.insertResult("test","test2","test3");

【问题讨论】:

  • 尽可能显示 DBHelper 类。

标签: java android sqlite android-studio


【解决方案1】:

数据库中不存在结果表。

您的DBHelper 似乎正在扩展SQLiteOpenHelper。您应该覆盖DBHelper 中的onCreate 方法。例如,添加如下内容:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table Results (_id integer primary key autoincrement, Question text not null, Rights text not null, Wrongs text not null);");
}

如果您更改了数据库架构,您还需要覆盖 onUpgrade 方法。

【讨论】:

  • 如果没有效果,请尝试卸载应用程序。
  • 即使从模拟器中完全卸载了应用程序?
  • @user998548 我不确定你的意思。卸载应用程序时,Android 会删除应用程序数据库。 onCreate 方法在创建数据库时运行。因此,重新安装应用程序会强制创建数据库并运行onCreate 方法。
  • 我正在卸载应用程序,但在 sqlite 浏览器中创建表后,重新安装应用程序,我收到错误,“表不存在;'不存在'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多