1

我在我的 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 个,就会发生崩溃。日志猫:

 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): 在 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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-18 16:54:15.126: E/AndroidRuntime(1258): 在 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): 在 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 个getReadableDatabase(SQLiteOpenHelper.java:188) 08-18 16:54:15.126: E/AndroidRuntime(1258): 在 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): at 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 更多getReadableDatabase(SQLiteOpenHelper.java:188) 08-18 16:54:15.126: E/AndroidRuntime(1258): 在 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): at 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 更多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 更多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 更多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 更多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 更多

4

1 回答 1

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));
于 2013-08-18T15:40:08.510 回答