这是我的SQLiteHandler类代码的一部分。
public class SQLiteHandler extends SQLiteOpenHelper implements DataKeyLists {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "users";
// Login table name
private static final String TABLE_USER = "user";
// query to create a login table
private static final String QUERY_CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USER
+ "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_UID + " TEXT UNIQUE,"
+ KEY_NAME + " TEXT,"
+ KEY_STATUS + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_GENDER + " TEXT,"
+ KEY_BIRTHDAY + " TEXT,"
+ KEY_RELIGION + " TEXT,"
+ KEY_NATIONALITY + " TEXT,"
+ KEY_COUNTRY_OF_RESIDENCE + " TEXT,"
+ KEY_CREATED_AT + " TEXT"
+ KEY_UPDATED_AT + " TEXT"
+ ")";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(QUERY_CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUserIntoSQLite(String uid, String name, String status, String email, String gender, String birthday, String created_at, String updated_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_UID, uid);
values.put(KEY_NAME, name);
values.put(KEY_STATUS, status);
values.put(KEY_EMAIL, email);
values.put(KEY_GENDER, gender);
values.put(KEY_BIRTHDAY, birthday);
values.put(KEY_CREATED_AT, created_at);
values.put(KEY_UPDATED_AT, updated_at);
long id = db.insert(TABLE_USER, null, values);
db.close();
Log.d(TAG, "New user is added into sqlite: " + id);
}
}
我后来添加了一个新列KEY_UPDATED_AT
QUERY_CREATE_LOGIN_TABLE
查询并重新启动了该应用程序
现在我在Logcat上收到以下消息。
Error inserting uid=564ef4c43388d0.82002082 birthday=1982-07-27 updated_at=null email=learn@android.com status=user name=learnandroid created_at=2015-11-20 05:24:04 gender=Male
11-20 15:41:42.976 8659-8852/? E/SQLiteDatabase: android.database.sqlite.SQLiteException: table user has no column named updated_at (code 1): , while compiling: INSERT INTO user(uid,birthday,updated_at,email,status,name,created_at,gender) VALUES (?,?,?,?,?,?,?,?)
如果存在版本差异,表格似乎会被删除并重新创建
我认为onUpgrade()
接受了这一部分,但为什么我仍然收到该错误消息?
我还在我的设备上卸载了应用程序,在Android Studio上清理并重建了项目并重新运行了应用程序。
这也没有用。
答案 0 :(得分:4)
你错过了一个逗号:
+ KEY_CREATED_AT + " TEXT"
+ KEY_UPDATED_AT + " TEXT"
必须是
+ KEY_CREATED_AT + " TEXT, "
+ KEY_UPDATED_AT + " TEXT"
然后卸载并重新安装您的应用程序(以便重新创建数据库) 或者只是增加DATABASE_VERSION常量值(即:将其设置为3)。