gpt4 book ai didi

android - 将字段添加到现有的 sqlite 数据库

转载 作者:太空狗 更新时间:2023-10-29 15:37:24 25 4
gpt4 key购买 nike

我已经发布了一个现有的应用程序,我想将位置坐标字段添加到 sqlite 数据库中。

我想知道是否可以在不在数据库中创建新表的情况下执行此操作。我不想覆盖用户现有的数据库条目,我只想为现有的数据库条目添加这个新字段并给它一个默认值。

这可能吗?

最佳答案

是的,

更新表时,您需要编写onUpgrade() 方法。目前,我使用以下命令创建一个包含新列的新表,并复制我当前的所有数据。希望您可以根据自己的代码进行调整。

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion);

db.beginTransaction();
try {
db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_UPGRADE);
List<String> columns = GetColumns(db, DATABASE_TABLE);
db.execSQL("ALTER table " + DATABASE_TABLE + " RENAME TO 'temp_" + DATABASE_TABLE + "'");
db.execSQL("create table " + DATABASE_UPGRADE);
columns.retainAll(GetColumns(db, DATABASE_TABLE));
String cols = join(columns, ",");
db.execSQL(String.format( "INSERT INTO %s (%s) SELECT %s from temp_%s", DATABASE_TABLE, cols, cols, DATABASE_TABLE));
db.execSQL("DROP table 'temp_" + DATABASE_TABLE + "'");
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
}

public static List<String> GetColumns(SQLiteDatabase db, String tableName) {
List<String> ar = null;
Cursor c = null;
try {
c = db.rawQuery("select * from " + tableName + " limit 1", null);
if (c != null) {
ar = new ArrayList<String>(Arrays.asList(c.getColumnNames()));
}
} catch (Exception e) {
Log.v(tableName, e.getMessage(), e);
e.printStackTrace();
} finally {
if (c != null)
c.close();
}
return ar;
}

public static String join(List<String> list, String delim) {
StringBuilder buf = new StringBuilder();
int num = list.size();
for (int i = 0; i < num; i++) {
if (i != 0)
buf.append(delim);
buf.append((String) list.get(i));
}
return buf.toString();
}

这包含 onUpgrade() 和两个辅助方法。 DATABASE_UPGRADE 是包含升级数据库的字符串:

private static final String DATABASE_UPGRADE =
"notes (_id integer primary key autoincrement, "
+ "title text not null, "
+ "body text not null, "
+ "date text not null, "
+ "edit text not null, "
+ "reminder text, "
+ "img_source text, "
+ "deletion, "
+ "priority)";

快速说明这是如何工作的:

  1. 检查表的当前版本是否已经存在(永远不应该存在),因为在这种情况下不应调用 onUpgrade()
  2. 由于失败,从当前表中获取列列表(使用辅助函数 GetColumns())。
  3. 将旧表重命名为 temp_OLDTABLENAME。
  4. 创建新版本的数据库附加列(当前为空)。
  5. 从旧版本的数据库中获取所有信息。
  6. 将其插入新数据库
  7. 删除旧表 (temp_OLDTABLENAME)。

我尝试编写足够通用的代码,所以我所要做的就是使用附加列更新 DATABASE_UPGRADE,这将处理所有其余部分。到目前为止,它已经通过 3 次升级为我工作。

关于android - 将字段添加到现有的 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5315997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com