MainActivity.class
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler db = new DatabaseHandler(this);
// Inserting Users
Log.d("Insert: ", "Inserting ..");
db.addContact(new Contact("Ravi", "kumar", 1, getDateTime()));
}
public String getDateTime() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return simpleDateFormat.format(date);
}
DatabaseHandler.class
public class DatabaseHandler extends SQLiteOpenHelper {
Context context;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "dbname";
private static final String TABLE_USERS = "users";
private static final String USERS_ID = "id";
private static final String USER_NAME = "name";
private static final String USER_LAST_NAME = "lastname";
private static final String USER_USER_LEVEL = "userlevel";
private static final String USER_LOGIN_TIME = "lastlogin";
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
+ USERS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + USER_NAME + " TEXT,"
+ USER_LAST_NAME + " TEXT NULL," + USER_USER_LEVEL + " INTEGER," + USER_LOGIN_TIME + " TEXT" + ")";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Toast.makeText(context,"Constructor is called", Toast.LENGTH_LONG).show();
System.out.println("Sql query : " +CREATE_USERS_TABLE);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USERS_TABLE);
Toast.makeText(context,"on Create is called", Toast.LENGTH_LONG).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i2) {
//db.execSQL("DROP TABLE IF EXISTS " +CREATE_USERS_TABLE);
db.execSQL("DROP TABLE IF EXISTS " +CREATE_USERS_TABLE );
onCreate(db);
Toast.makeText(context,"on Upgrade is called", Toast.LENGTH_LONG).show();
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USER_NAME, contact.get_name()); // Contact Name
values.put(USER_LAST_NAME, contact.get_lname());
values.put(USER_USER_LEVEL, contact.get_level()); // Contact Phone Number
values.put(USER_LOGIN_TIME,contact.get_lastlogin());
//Inserting Row
db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
}
.....
..
上面的代码创建了一个数据库,表并插入了数据。但是,如果我更改架构,例如将lastlogin
更改为lastlogiin
,
private static final String USER_LOGIN_TIME = "lastlogiin";
它没有生效。然后,我将数据库版本更改为
private static final int DATABASE_VERSION = 2;
但是,它会引发错误。
Logcat:
at dalvik.system.NativeStart.main(Native Method) Caused by:
android.database.sqlite.SQLiteException: near "CREATE": syntax error:
, while compiling: DROP TABLE IF EXISTS CREATE TABLE users(id INTEGER
PRIMARY KEY AUTOINCREMENT,name TEXT,lastname TEXT NULL,userlevel
INTEGER,lastlogiin TEXT)
答案 0 :(得分:5)
@ user3289108替换 onUpgrade()方法中的以下行
db.execSQL("DROP TABLE IF EXISTS " +CREATE_USERS_TABLE );
要
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS );