SQLite列错误:表XXXX没有名为ZZZZ的列

时间:2019-03-02 20:02:49

标签: android sqlite

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

这是在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)

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

您的问题很可能是关于 onCreate 方法的误解。就是说 onCreate 不会在每次运行该应用程序时运行。 onCreate 仅在数据库实际上不存在时运行。如果该数据库已经由该应用的其他先前运行创建过,则不会运行 onCreate 方法。

因此,将不会应用通常在 onCreate 方法中应用的对数据库结构所做的任何更改。

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

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

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

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