【问题标题】:SQLite Column Error : table XXXX has no column named ZZZZSQLite 列错误:表 XXXX 没有名为 ZZZZ 的列
【发布时间】:2019-07-24 13:00:09
【问题描述】:

我正在尝试在包含两行的学生表中插入行:ID 和名称

这是在 MyDBHandler 类中实现的 addHandler 函数:

 public void addHandler(Student student) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_ID, student.getID());
    values.put(COLUMN_NAME, student.getStudentName());
    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(TABLE_NAME, null, values);
    db.close();
}

onCreate 方法是:-

public void onCreate(SQLiteDatabase db) { 
    String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT )"; 
    db.execSQL(CREATE_TABLE); 
}

扩展 SQLiteOpenHelper 的 MyDBHandler 类的属性:

 private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "studentDB.db";
public static final String TABLE_NAME = "Student";
public static final String COLUMN_ID = "StudentID";
public static final String COLUMN_NAME = "StudentName";

我的 activity_main.xml 文件中有一个添加按钮,下面是代码:

public void add(View view){
    MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
    int id = Integer.parseInt(studentIdText.getText().toString());
    String name = studentNameText.getText().toString();
    Student student = new Student(id, name);
    dbHandler.addHandler(student);
    studentIdText.setText("");
    studentNameText.setText("");
}

应用程序运行良好,但是当我想在表格中插入一行时,我在运行选项卡中收到以下错误:

E/SQLiteLog: (1) table Student has no column named StudentID
E/SQLiteDatabase: Error inserting StudentName=yassine StudentID=10

android.database.sqlite.SQLiteException: table Student has no column named StudentID (code 1): , while compiling: INSERT INTO Student(StudentName,StudentID) VALUES (?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
    (table Student has no column named StudentID (code 1): , while compiling: INSERT INTO Student(StudentName,StudentID) VALUES (?,?))
#################################################################
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1607)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
    at com.example.test.MyDBHandler.addHandler(MyDBHandler.java:48)
    at com.example.test.MainActivity.add(MainActivity.java:40)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
    at android.view.View.performClick(View.java:5246)
    at android.widget.TextView.performClick(TextView.java:10566)
    at android.view.View$PerformClick.run(View.java:21256)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6917)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

有什么建议吗?

【问题讨论】:

  • 我的坏!不会再这样做了!
  • 您是如何创建表格的?
  • 您好,我使用了 onCreate 方法
  • public void onCreate(SQLiteDatabase db) { String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT )"; db.execSQL(CREATE_TABLE); }
  • 尝试卸载应用程序并重新运行,因为您可能已经更改了结构(例如,添加了 StudentId 列)。 onCreate 只在创建数据库时运行一次,而不是每次运行应用程序时运行一次,因此所做的更改不会被应用,除非你强制 onCreate 运行。

标签: android sqlite


【解决方案1】:

您的问题很可能是对 onCreate 方法的误解。那就是 onCreate 不会在每次运行应用程序时运行。 onCreate 只会在数据库实际不存在时运行。如果该数据库已由应用程序的另一个先前运行创建,则 onCreate 方法不会运行。

因此,对数据库结构所做的任何更改(通常应用于 onCreate 方法)都不会被应用。

在应用程序运行后,您似乎已将 StudentId 列的定义添加到 onCreate 方法的代码中。

只要您没有需要保留的数据(很可能),那么最简单的解决方案就是执行以下操作之一:-

  • 删除或清除应用程序的数据(通过设置/应用程序)
  • 卸载应用程序

然后重新运行应用程序,然后将根据修改后的onCreate代码使用代码创建数据库。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多