android版本数据库表,android - Android:升级数据库版本并添加新表 - 堆栈内存溢出...

@ jkschneider的回答是对的。 然而,有一个更好的方法。

from_1_to_2.sql

ALTER TABLE books ADD COLUMN book_rating INTEGER;

from_2_to_3.sql

ALTER TABLE books RENAME TO book_information;

from_3_to_4.sql

ALTER TABLE book_information ADD COLUMN calculated_pages_times_rating INTEGER;

UPDATE book_information SET calculated_pages_times_rating = (book_pages * book_rating) ;

这些.sql文件将根据数据库的版本在onUpgrade()方法中执行。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 4;

private static final String DATABASE_NAME = "database.db";

private static final String TAG = DatabaseHelper.class.getName();

private static DatabaseHelper mInstance = null;

private final Context context;

private DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

this.context = context;

}

public static synchronized DatabaseHelper getInstance(Context ctx) {

if (mInstance == null) {

mInstance = new DatabaseHelper(ctx.getApplicationContext());

}

return mInstance;

}

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);

// The rest of your create scripts go here.

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);

// You will not need to modify this unless you need to do some android specific things.

// When upgrading the database, all you need to do is add a file to the assets folder and name it:

// from_1_to_2.sql with the version that you are upgrading to as the last version.

try {

for (int i = oldVersion; i < newVersion; ++i) {

String migrationName = String.format("from_%d_to_%d.sql", i, (i + 1));

Log.d(TAG, "Looking for migration file: " + migrationName);

readAndExecuteSQLScript(db, context, migrationName);

}

} catch (Exception exception) {

Log.e(TAG, "Exception running upgrade script:", exception);

}

}

@Override

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

private void readAndExecuteSQLScript(SQLiteDatabase db, Context ctx, String fileName) {

if (TextUtils.isEmpty(fileName)) {

Log.d(TAG, "SQL script file name is empty");

return;

}

Log.d(TAG, "Script found. Executing...");

AssetManager assetManager = ctx.getAssets();

BufferedReader reader = null;

try {

InputStream is = assetManager.open(fileName);

InputStreamReader isr = new InputStreamReader(is);

reader = new BufferedReader(isr);

executeSQLScript(db, reader);

} catch (IOException e) {

Log.e(TAG, "IOException:", e);

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

Log.e(TAG, "IOException:", e);

}

}

}

}

private void executeSQLScript(SQLiteDatabase db, BufferedReader reader) throws IOException {

String line;

StringBuilder statement = new StringBuilder();

while ((line = reader.readLine()) != null) {

statement.append(line);

statement.append("\n");

if (line.endsWith(";")) {

db.execSQL(statement.toString());

statement = new StringBuilder();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值