当 ORMLite 中的架构更改时删除列
Drop a column when the schema changes in ORMLite
我正在使用 Android 和 ORMLite。我是新手,我将数据库升级到版本 2。我有 class“自动”,需要删除别名列。
@DatabaseTable(tableName = "Auto")
public class Auto {
public static final String ALIAS = "alias";
public static final String PLACA = "placa";
@DatabaseField(generatedId = true, columnName = "ID")
private int id;
@DatabaseField(canBeNull = false, columnName = ALIAS)
private String alias;
@DatabaseField(canBeNull = false, columnName = PLACA)
public Auto()
{
//ORMLite needs a no-arg constructor
}
public Auto(String alias, String placa) {
this.alias = alias;
this.placa = placa;
}
…..
…..
}
我将 class 自动更改为...
@DatabaseTable(tableName = "Auto")
public class Auto {
public static final String PLACA = "placa";
@DatabaseField(generatedId = true, columnName = "ID")
private int id;
@DatabaseField(canBeNull = false, columnName = PLACA)
public Auto()
{
//ORMLite needs a no-arg constructor
}
public Auto(String alias, String placa) { this.placa = placa;
}
…..
…..
}
我需要知道 ORMLite 是否在执行“onUpgradeMethod”时自动删除别名列,或者我必须像这样手动执行。
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
if(oldVersion == 1) {
try {
Dao dao;
dao = getAutoDao();
dao.executeRaw("ALTER TABLE `Auto` DROP COLUMN alias;");
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Error ", e);
}
}
}
这个链接帮我解决了。
Drop column in SQLite
Delete column from SQL
save data before table upgrade
Drop table if it already exists and then re-create it?
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names
"a", "b", and "c" and that you want to delete column "c" from this
table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1; CREATE TABLE t1(a,b); INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;
根据菲尔的评论,可能的解决方案是这样的。 4
So your choices are:
leave things as they are,
add a new column, copying all the data from old to new as part of the upgrade, and just ignore the old column altogether, or
Use a drop/create strategy to upgrade: back up the data from table into a temporary table, drop the table, re-create it as you'd like it
to be, copy all the data back into it, and finally drop the temporary
table.
Database upgrades are always nervous affairs (need lots of error
handling, lots of testing), frankly if the name is the only thing that
concerns you, I'd leave it alone (option 1). If you really need to
change it then option 2 is low risk but leaves a 'dead' column and
data lying around. Option 3 may be your choice if, for example, the
data is a significant percentage of the overall database size.
我决定从列中删除数据但不删除列,这与解决方案 2 类似。
我正在使用 Android 和 ORMLite。我是新手,我将数据库升级到版本 2。我有 class“自动”,需要删除别名列。
@DatabaseTable(tableName = "Auto")
public class Auto {
public static final String ALIAS = "alias";
public static final String PLACA = "placa";
@DatabaseField(generatedId = true, columnName = "ID")
private int id;
@DatabaseField(canBeNull = false, columnName = ALIAS)
private String alias;
@DatabaseField(canBeNull = false, columnName = PLACA)
public Auto()
{
//ORMLite needs a no-arg constructor
}
public Auto(String alias, String placa) {
this.alias = alias;
this.placa = placa;
}
…..
…..
}
我将 class 自动更改为...
@DatabaseTable(tableName = "Auto")
public class Auto {
public static final String PLACA = "placa";
@DatabaseField(generatedId = true, columnName = "ID")
private int id;
@DatabaseField(canBeNull = false, columnName = PLACA)
public Auto()
{
//ORMLite needs a no-arg constructor
}
public Auto(String alias, String placa) { this.placa = placa;
}
…..
…..
}
我需要知道 ORMLite 是否在执行“onUpgradeMethod”时自动删除别名列,或者我必须像这样手动执行。
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
if(oldVersion == 1) {
try {
Dao dao;
dao = getAutoDao();
dao.executeRaw("ALTER TABLE `Auto` DROP COLUMN alias;");
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Error ", e);
}
}
}
这个链接帮我解决了。
Drop column in SQLite
Delete column from SQL
save data before table upgrade
Drop table if it already exists and then re-create it?
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table. If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1; CREATE TABLE t1(a,b); INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;
根据菲尔的评论,可能的解决方案是这样的。 4
So your choices are:
leave things as they are,
add a new column, copying all the data from old to new as part of the upgrade, and just ignore the old column altogether, or
Use a drop/create strategy to upgrade: back up the data from table into a temporary table, drop the table, re-create it as you'd like it to be, copy all the data back into it, and finally drop the temporary table.
Database upgrades are always nervous affairs (need lots of error handling, lots of testing), frankly if the name is the only thing that concerns you, I'd leave it alone (option 1). If you really need to change it then option 2 is low risk but leaves a 'dead' column and data lying around. Option 3 may be your choice if, for example, the data is a significant percentage of the overall database size.
我决定从列中删除数据但不删除列,这与解决方案 2 类似。