我在我的SQLite数据库中做了ALTER TABLE,现在更新我的应用程序一切顺利,但是如果你在没有安装旧版本数据库的情况下启动新APK就不会创建新列!为什么? 这是我正在使用的代码:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion >=2) {
db.execSQL("ALTER TABLE Giornate ADD COLUMN note TEXT");
}
}
在我的帮助器类中,“Giornate”中有几个表我必须添加“NOTE”字段
@Override
public void onCreate(SQLiteDatabase db) {
BilancioHelper.db = db;
//tabella giornate
StringBuilder builder = new StringBuilder();
builder.append("CREATE TABLE {0} (");
builder.append("{1} INTEGER PRIMARY KEY AUTOINCREMENT,");
builder.append("{2} TEXT,");
builder.append("{3} REAL DEFAULT 0,");
builder.append("{4} REAL DEFAULT 0,");
builder.append("{5} REAL DEFAULT 0,");
builder.append("{6} INTEGER,"); // E' boolean
builder.append("{7} INTEGER"); // E' boolean
builder.append(");");
db.execSQL(MessageFormat.format(builder.toString(),
GiornateTable.TABLE_NAME, GiornateTable._ID,
GiornateTable.DATA, GiornateTable.ENTRATA,
GiornateTable.USCITA, GiornateTable.BANCA, GiornateTable.RIPOSO, GiornateTable.SPESA));
的字符串
public interface GiornateTable extends BaseColumns {
public static final String TABLE_NAME = "Giornate";
public static final String ENTRATA = "entrata";
public static final String DATA = "data";
public static final String USCITA = "uscita";
public static final String BANCA = "banca";
public static final String RIPOSO = "riposo";
public static final String SPESA = "spesa";
public static final String NOTE = "note";
public static final String[] COLUMNS = {_ID, ENTRATA, DATA, USCITA, BANCA, RIPOSO, NOTE};
}
然后,我做了这些测试。启动版本1和2然后一切顺利。但是,如果我只投了2个则会发生崩溃。 logcat的:
08-18 16:54:15.126: E/AndroidRuntime(1258): java.lang.RuntimeException: Unable to start activity ComponentInfo{bilancio.personale/it.della90.bilancio.MainActivity}: java.lang.NullPointerException
08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.ActivityThread.access $ 600(ActivityThread.java:141) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.os.Handler.dispatchMessage(Handler.java:99) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.os.Looper.loop(Looper.java:137) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.ActivityThread.main(ActivityThread.java:5041) 08-18 16:54:15.126:E / AndroidRuntime(1258):at java.lang.reflect.Method.invokeNative(Native Method) 08-18 16:54:15.126:E / AndroidRuntime(1258):at java.lang.reflect.Method.invoke(Method.java:511) 08-18 16:54:15.126:E / AndroidRuntime(1258):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 08-18 16:54:15.126:E / AndroidRuntime(1258):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-18 16:54:15.126:E / AndroidRuntime(1258):at dalvik.system.NativeStart.main(Native Method) 08-18 16:54:15.126:E / AndroidRuntime(1258):引起:java.lang.NullPointerException 08-18 16:54:15.126:E / AndroidRuntime(1258):at it.della90.bilancio.db.BilancioHelper.onCreate(BilancioHelper.java:38) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188) 08-18 16:54:15.126:E / AndroidRuntime(1258):at it.della90.bilancio.MainActivity.calcolaTotaleRisparmioMese(MainActivity.java:342) 08-18 16:54:15.126:E / AndroidRuntime(1258):at it.della90.bilancio.MainActivity.onCreate(MainActivity.java:112) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.Activity.performCreate(Activity.java:5104) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 08-18 16:54:15.126:E / AndroidRuntime(1258):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 08-18 16:54:15.126:E / AndroidRuntime(1258):... 11更多
答案 0 :(得分:0)
您可以使用SharedPreferences检查他是否使用新版本的数据库!
在onCreate方法中:
SharedPreferences shared = getSharedPreferences("SYNC_PREFERENCES", Context.MODE_PRIVATE);
boolean flag = shared.getBoolean("new_version_database",true);
if(flag){
//call your query with the ALTER TABLE in it
shared.edit().putBoolean("new_version_database",false).commit();
}
在任何其他情况下,您将遵循普通的数据库升级!
将自己的代码风格应用到上面,你就可以了!
修改强>
@Override
public void onCreate(SQLiteDatabase db) {
BilancioHelper.db = db;
//tabella giornate
StringBuilder builder = new StringBuilder();
builder.append("CREATE TABLE {0} (");
builder.append("{1} INTEGER PRIMARY KEY AUTOINCREMENT,");
builder.append("{2} TEXT,");
builder.append("{3} REAL DEFAULT 0,");
builder.append("{4} REAL DEFAULT 0,");
builder.append("{5} REAL DEFAULT 0,");
builder.append("{6} INTEGER,"); // E' boolean
builder.append("{7} INTEGER");
builder.append("{8} TEXT");
builder.append(");");
db.execSQL(MessageFormat.format(builder.toString(),
GiornateTable.TABLE_NAME, GiornateTable._ID,
GiornateTable.DATA, GiornateTable.ENTRATA,
GiornateTable.USCITA, GiornateTable.BANCA, GiornateTable.RIPOSO, GiornateTable.SPESA, GiornateTable.NOTE));