我无法在现有数据库中添加2列。我阅读了网络上的任何主题,但我无法解决我的问题。我可以对我的问题有一个完整的个性化答案吗? 我自然不想失去现有记录,只是改变它! 我想添加2列(电话(整数),automex(文本))到以这种方式创建的数据库..
package com.ozzem.mybirthdaylite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class dbMyBirthday {
SQLiteDatabase db;
private Context mContext;
private DbHelper mDbHelper;
private static final String DB_NAME="dbmyBirthday";
private static final int DB_VERSION=1;
//costruttore crea anche il DbHelper
public dbMyBirthday(Context ctx){
mContext=ctx;
mDbHelper=new DbHelper(ctx, DB_NAME, null, DB_VERSION);
}
public void open(){ //il database su cui agiamo è leggibile/scrivibile
db=mDbHelper.getWritableDatabase();
}
public void close(){ //chiudiamo il database su cui agiamo
db.close();
}
public void deletePerson(int idDel){ //metodo per inserire i dati
String where = "id = " + idDel;
db.delete(Person.PERSON_TABLE, where,null);
// Log.d("db_call", "Record of id["+idDel+"] deleted");
}
public void insertPerson(long l,String name,String birthday){ //metodo per inserire i dati
ContentValues cv=new ContentValues();
cv.put(Person.UID, l);
cv.put(Person.NAME, name);
cv.put(Person.BIRTHDAY, birthday);
db.insert(Person.PERSON_TABLE, null, cv);
// Log.d("db_call", "Record of uid["+uid+"] insered");
}
public boolean exist(long uid){
boolean ex = false;
Cursor c= db.rawQuery("SELECT COUNT(*) FROM " + Person.PERSON_TABLE + " WHERE uid = "+ "'" + uid + "'", null);
c.moveToFirst();
int jcount = c.getInt(0);
if (jcount >0){
ex=true;
}
return ex;
}
public void updatePersonFromId(int id,String name,String birthday){
ContentValues cv=new ContentValues();
// cv.put(Person.UID, id);
cv.put(Person.NAME, name);
cv.put(Person.BIRTHDAY, birthday);
String where = "id = " + "'"+id+"'";
db.update(Person.PERSON_TABLE, cv, where, null);
// Log.d("db_call", "Record of id["+id+"] updated");
}
public void updatePersonFromUid(long l,String name,String birthday){
ContentValues cv=new ContentValues();
cv.put(Person.UID, l);
cv.put(Person.NAME, name);
cv.put(Person.BIRTHDAY, birthday);
String where = "uid = " + "'"+l+"'";
db.update(Person.PERSON_TABLE, cv, where, null);
// Log.d("db_call", "Record of uid["+uid+"] updated");
}
public Cursor fetchAllBirthday(){ //metodo per fare la query di tutti i dati
// Log.d("db_call", "Fetching all birthdays");
return db.query(Person.PERSON_TABLE, null,null,null,null,null,null);
}
public static class Person { // i metadati della tabella, accessibili ovunque
static final String PERSON_TABLE = "person";
static final String ID = "id";
static final String UID = "uid";
static final String NAME = "name";
public static final String BIRTHDAY = "birthday";
}
private static final String PERSON_TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " //codice sql di creazione della tabella
+ Person.PERSON_TABLE + " ("
+ Person.ID + " integer primary key autoincrement, "
+ Person.NAME + " text not null, "
+ Person.UID + " integer not null, "
+ Person.BIRTHDAY + " text not null );";
private class DbHelper extends SQLiteOpenHelper { //classe che ci aiuta nella creazione del db
public DbHelper(Context context, String name, CursorFactory factory,int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase _db) {
// TODO Auto-generated method stub
_db.execSQL(PERSON_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:0)
将 DB_Version 增加到 2 ,然后在您的 OnUpgrade 覆盖OpenHelper中运行升级代码
例如:
if (oldVersion==1 && newVersion==2) {
db.execSQL("ALTER TABLE " + TableName + " ADD COLUMN " + ColumnName1);
db.execSQL("ALTER TABLE " + TableName + " ADD COLUMN " + ColumnName2);
}
同时修改您的初始代码,以便为那些没有要升级的版本1数据库的人创建新字段。即那些首先创建第2版数据库的人。
答案 1 :(得分:0)
@ozzem以下是您需要在应用程序Start Class上执行的一些步骤。
步骤1.检查数据库中是否已存在2列?见这里SO链接是here
步骤2.在确认运行SQL查询后,确保数据库中没有2列要添加
ALTER TABLE PERSON_TABLE ADD phone INTEGER DEFAULT 0
ALTER TABLE PERSON_TABLE ADD automex TEXT
当您在表格中添加时,您也可以在此列中提供默认值。