SQLiteException:表没有列

时间:2017-02-27 16:19:47

标签: android android-sqlite

嘿伙计们,我在sqlexception有一个问题。我正在做的是我为我的数据库添加一列,然后这发生了如何解决这个问题?你可以帮我在android开发中帮我新手。

我的sqlitehelper

public class SQLiteHelper extends SQLiteOpenHelper {

static String DATABASE_NAME="AndroidJSonDataBase";

public static final String TABLE_NAME="AndroidJSonTable";

public static final String Table_Column_ID="id";

public static final String Table_Column_1_Name="name";

public static final String Table_Column_2_PhoneNumber="phone_number";

public static final String Table_Column_3_Birth="birth";
public static final String Table_Column_4_Course="course";
public static final String Table_Column_5_Email="email";
public SQLiteHelper(Context context) {

    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase database) {

    String CREATE_TABLE="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+Table_Column_ID+" INTEGER PRIMARY KEY,"+Table_Column_3_Birth+"VARCHAR,"+Table_Column_4_Course+"VACHAR,"+Table_Column_5_Email+"VARCHAR, "+Table_Column_1_Name+" VARCHAR, "+Table_Column_2_PhoneNumber+" VARCHAR)";
    database.execSQL(CREATE_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);

}

}

我的logcat

02-28 00:08:46.019 4298-4298/com.example.lightning.beats E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.lightning.beats, PID: 4298
                                                                        android.database.sqlite.SQLiteException: table AndroidJSonTable has no column named birth (code 1): , while compiling: INSERT INTO AndroidJSonTable (name,phone_number,birth,course,email) VALUES('ghh','556','555', 'gg','ggg');
                                                                            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:891)
                                                                            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:502)
                                                                            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
                                                                            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
                                                                            at com.example.lightning.hannah.Basura.prof.InsertDataIntoSQLiteDatabase(prof.java:99)
                                                                            at com.example.lightning.hannah.Basura.prof$1.onClick(prof.java:48)
                                                                            at android.view.View.performClick(View.java:4452)
                                                                            at android.view.View$PerformClick.run(View.java:18451)
                                                                            at android.os.Handler.handleCallback(Handler.java:733)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:136)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5421)
                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                            at java.lang.reflect.Method.invoke(Method.java:515)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:979)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
                                                                            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

只需从您的设备卸载您的应用程序,然后重新安装即可重新安装。这里的问题是您之后已将新列添加到代码中,但设备中已存在的实际数据库没有此类列。所以你需要再次创建数据库。

一种更好的方法是,在onUpdate方法中删除并重新创建所有表,并在每次更改架构时增加db版本。