我有一个在数据库中有几个表的应用程序。我要升级2个表,因为我正在为它们添加一个额外的字段。我希望其他表保持不变。
到目前为止,我已经覆盖了SQLiteOpenHelper类的onUpgrade方法。我想要升级的2个表是TRANSACTIONS表和COMPANYID表。我在onUpgrade中执行此操作,重新命名表,使用新字段重新创建它们,然后复制旧数据。
这很好用。
我遇到的问题是我安装升级时其他表不存在。我不确定升级是如何工作的,但是一旦我改变了我需要的表格,我就不会在onUpgrade中明确地调用onCreate。
我试过从onUpgrade中调用onCreate,但后来我得到一个错误,说明TRANSACTIONS表已经存在,我希望因为我刚刚在onUpgrade中创建它。
如何正确完成? 我是应该从onUpgrade中调用onCreate,还是在onCreate执行时进行测试以查看表是否已存在?
注意我正在递增版本号。
先谢谢,马特。
private class DBHelper extends SQLiteOpenHelper {
// database name and version number
public static final String DB_NAME = "carefreemobiledb.db";
public static final int DB_VERSION = 54;
// table names
public static final String TABLETRANSACTIONS = "transactions";
public static final String TABLECARER = "carer";
public static final String TABLETRANSACTIONSMAP = "transactionsmap";
public static final String TABLEPHONE = "phone";
public static final String TABLECOMPANYID = "companyid";
public static final String TABLEBACKGROUNDSERVICES = "backgroundservices";
public static final String TABLEMESSAGE = "message";
public static final String TABLEDUPLICATETRANSACTIONS = "tableduplicatetransactions";
public static final String TABLECACHEDROTA = "tablecachedrota";
public static final String TABLEUSERS = "users";
public DBHelper() {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sqlToCreateTransactionsTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s INT, %s TEXT, %s TEXT, %s TEXT )",
TABLETRANSACTIONS, C_ID, C_TYPE, C_COMPANY_ID,
C_PERSON_ID, C_NAME, C_TAG_ID, C_STATUS,
C_TAG_SCAN_TIME, C_TAG_SENTSERVER_TIME,
C_TRANSACTIONS_LATITUDE, C_TRANSACTIONS_LONGITUDE,
C_DRIVER);
db.execSQL(sqlToCreateTransactionsTable);
Log.e(TAG, "oncreate " + sqlToCreateTransactionsTable);
String sqlToCreateCarerTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TABLECARER, C_ID_INDEX, C_CARER_ID, C_COMP_ID,
C_CARER_FIRSTNAME, C_CARER_LASTNAME, C_PASSWORD,
C_DATE_TIME);
db.execSQL(sqlToCreateCarerTable);
Log.e(TAG, "oncreate " + sqlToCreateCarerTable);
String sqlToCreateTransactionsmapTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TABLETRANSACTIONSMAP, C_ID, C_TYPE, C_COMPANY_ID,
C_PERSON_ID, C_NAME, C_TAG_ID, C_STATUS,
C_TAG_SCAN_TIME, C_LONGITUDE, C_LATITUDE);
db.execSQL(sqlToCreateTransactionsmapTable);
Log.e(TAG, "oncreate " + sqlToCreateTransactionsmapTable);
String sqlToCreatePhone = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLEPHONE, C_ID_PHONE, C_PHONE_NAME,
C_PHONE_NUMBER);
db.execSQL(sqlToCreatePhone);
Log.e(TAG, "oncreate " + sqlToCreatePhone);
String sqlToCreateUsers = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLEUSERS, C_ID_USERS, C_USER_NAME, C_USER_ID);
db.execSQL(sqlToCreateUsers);
Log.e(TAG, "oncreate " + sqlToCreateUsers);
String sqlToCreateMessage = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TABLEMESSAGE, C_ID_MESSAGE, C_MESSAGE_GUID,
C_MESSAGE_TEXT, C_MESSAGE_CREATED_AT,
C_MESSAGE_SENDER, C_MESSAGE_REPLIED,
C_MESSAGE_SEEN, C_MESSAGE_DISPLAYED,
C_MESSAGE_REPLY_MESSAGE, C_MESSAGE_IS_STANDALONE);
db.execSQL(sqlToCreateMessage);
Log.e(TAG, "oncreate " + sqlToCreateMessage);
String sqlToCreateBackgroundServices = String.format(
"create table %s ( %s INTEGER primary key, %s TEXT)",
TABLEBACKGROUNDSERVICES, C_ID_BACKGROUNDSERVICES,
C_BACKGROUNDSERVICES_HAVE_RUN_ONCE);
db.execSQL(sqlToCreateBackgroundServices);
Log.e(TAG, "oncreate " + sqlToCreateBackgroundServices);
String sqlToCreateCompanyId = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT)", TABLECOMPANYID,
C_ID_COMPANY_ID_OUTSIDE_APP_PURPOSES,
C_COMPANY_ID_OUTSIDE_APP_PURPOSES,
C_COMPANY_TAG_TOUCH_INTERVAL,
C_COMPANY_SAVE_PASSWORD,
C_COMPANY_ALLOW_CHANGE_USER,
C_COMPANY_DISPLAY_ROTA_DETAILS,
C_COMPANY_DISPLAY_CLIENT_PHONENUMBER,
C_COMPANY_DISPLAY_CLIENTKEYSAFE,
C_COMPANY_DISPLAY_DOUBLEUP_CARER,
C_COMPANY_DISPLAY_CONTACTS, C_COMPANY_AUTOLOGOUT,
C_COMPANY_DISPLAY_NOTES, C_COMPANY_DISPLAY_MEDS,
C_COMPANY_ROTALOGOUT,
C_COMPANY_DISPLAY_ACTUAL_TIME,
C_COMPANY_QRCODEENABLED, C_COMPANY_MANUAL_INPUT,
C_COMPANY_ROTAS_ONLY, C_COMPANY_COMPNAME,
C_COMPANY_COMP_URL, C_COMPANY_CALC_GPS_COORDS,
C_COMPANY_SYSTEM_EXTERNAL_TIME_TOLERANCE,
C_COMPANY_CARER_REPORTING,
C_COMPANY_SHOW_DIRECTIONS, C_COMPANY_LONE_WORKER,
C_COMPANY_SHOW_DELETE_BUTTON,
C_COMPANY_CARER_MESSAGING,
C_COMPANY_CANNOT_SIGN_IN_INTERVAL,
C_FORMATTED_TAGS_ONLY, C_LOG_TO_SDCARD,
C_SHOWGENERALNOTES, C_SHOWENTRYMETHOD,
C_PROMPTFORDRIVER);
db.execSQL(sqlToCreateCompanyId);
Log.e(TAG, "oncreate " + sqlToCreateCompanyId);
String sqlToCreateDuplicateTransactions = String.format(
"create table %s ( %s INTEGER primary key, %s TEXT)",
TABLEDUPLICATETRANSACTIONS, C_ID_DUPLICATETRANSACTIONS,
C_DUPLICATETRANSACTIONS);
db.execSQL(sqlToCreateDuplicateTransactions);
Log.e(TAG, "oncreate " + sqlToCreateDuplicateTransactions);
String sqlToCreateCachedRota = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLECACHEDROTA, C_ID_CACHED_ROTA,
C_CACHED_ROTA_DATE, C_CACHED_ROTA);
db.execSQL(sqlToCreateCachedRota);
Log.e(TAG, "oncreate " + sqlToCreateCachedRota);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//copy transaction data
db.execSQL("ALTER TABLE "+TABLETRANSACTIONS+" RENAME TO Old"+TABLETRANSACTIONS);
String sqlToCreateTransactionsTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s INT, %s TEXT, %s TEXT, %s TEXT )",
TABLETRANSACTIONS, C_ID, C_TYPE, C_COMPANY_ID,
C_PERSON_ID, C_NAME, C_TAG_ID, C_STATUS,
C_TAG_SCAN_TIME, C_TAG_SENTSERVER_TIME,
C_TRANSACTIONS_LATITUDE, C_TRANSACTIONS_LONGITUDE, C_DRIVER);
db.execSQL(sqlToCreateTransactionsTable);
Log.e(TAG, "onUpgrade " + sqlToCreateTransactionsTable);
db.execSQL("INSERT INTO " + TABLETRANSACTIONS + " (" + C_ID+", " + C_TYPE +", " + C_COMPANY_ID+", " + C_PERSON_ID + ", " + C_NAME+", " + C_TAG_ID+", " + C_STATUS + ", " +C_TAG_SCAN_TIME +", " +
C_TAG_SENTSERVER_TIME +", "+ C_TRANSACTIONS_LATITUDE+", " +C_TRANSACTIONS_LONGITUDE +" ) SELECT " +C_ID +", " +C_TYPE+ ", " +C_COMPANY_ID+", " +
C_PERSON_ID+", " +C_NAME+", " + C_TAG_ID+", " +C_STATUS+", "+C_TAG_SCAN_TIME+", "+C_TAG_SENTSERVER_TIME+", "+ C_TRANSACTIONS_LATITUDE+", "+C_TRANSACTIONS_LONGITUDE+" FROM Old"+TABLETRANSACTIONS);
db.execSQL("DROP TABLE Old"+TABLETRANSACTIONS);
Log.e(TAG, " SQLiteOpenHelper onUpgrade: finished copying old data to new transaction table");
//copy company option table data
db.execSQL("ALTER TABLE "+TABLECOMPANYID+" RENAME TO Old"+TABLECOMPANYID);
String sqlToCreateCompanyId = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT)", TABLECOMPANYID,
C_ID_COMPANY_ID_OUTSIDE_APP_PURPOSES,
C_COMPANY_ID_OUTSIDE_APP_PURPOSES,
C_COMPANY_TAG_TOUCH_INTERVAL,
C_COMPANY_SAVE_PASSWORD,
C_COMPANY_ALLOW_CHANGE_USER,
C_COMPANY_DISPLAY_ROTA_DETAILS,
C_COMPANY_DISPLAY_CLIENT_PHONENUMBER,
C_COMPANY_DISPLAY_CLIENTKEYSAFE,
C_COMPANY_DISPLAY_DOUBLEUP_CARER,
C_COMPANY_DISPLAY_CONTACTS, C_COMPANY_AUTOLOGOUT,
C_COMPANY_DISPLAY_NOTES, C_COMPANY_DISPLAY_MEDS,
C_COMPANY_ROTALOGOUT,
C_COMPANY_DISPLAY_ACTUAL_TIME,
C_COMPANY_QRCODEENABLED, C_COMPANY_MANUAL_INPUT,
C_COMPANY_ROTAS_ONLY, C_COMPANY_COMPNAME,
C_COMPANY_COMP_URL, C_COMPANY_CALC_GPS_COORDS,
C_COMPANY_SYSTEM_EXTERNAL_TIME_TOLERANCE,
C_COMPANY_CARER_REPORTING,
C_COMPANY_SHOW_DIRECTIONS, C_COMPANY_LONE_WORKER,
C_COMPANY_SHOW_DELETE_BUTTON,
C_COMPANY_CARER_MESSAGING,
C_COMPANY_CANNOT_SIGN_IN_INTERVAL,
C_FORMATTED_TAGS_ONLY, C_LOG_TO_SDCARD,
C_SHOWGENERALNOTES, C_SHOWENTRYMETHOD,
C_PROMPTFORDRIVER);
db.execSQL(sqlToCreateCompanyId);
Log.e(TAG, "onUpgrade " + sqlToCreateCompanyId);
db.execSQL("INSERT INTO " + TABLECOMPANYID + " (" + C_ID_COMPANY_ID_OUTSIDE_APP_PURPOSES + "," +
C_COMPANY_ID_OUTSIDE_APP_PURPOSES +"," +
C_COMPANY_TAG_TOUCH_INTERVAL +"," +
C_COMPANY_SAVE_PASSWORD +"," +
C_COMPANY_ALLOW_CHANGE_USER +"," +
C_COMPANY_DISPLAY_ROTA_DETAILS +"," +
C_COMPANY_DISPLAY_CLIENT_PHONENUMBER +"," +
C_COMPANY_DISPLAY_CLIENTKEYSAFE +"," +
C_COMPANY_DISPLAY_DOUBLEUP_CARER +"," +
C_COMPANY_DISPLAY_CONTACTS+"," +
C_COMPANY_AUTOLOGOUT +"," +
C_COMPANY_DISPLAY_NOTES+"," +
C_COMPANY_DISPLAY_MEDS +"," +
C_COMPANY_ROTALOGOUT +"," +
C_COMPANY_DISPLAY_ACTUAL_TIME +"," +
C_COMPANY_QRCODEENABLED+"," +
C_COMPANY_MANUAL_INPUT +"," +
C_COMPANY_ROTAS_ONLY+"," +
C_COMPANY_COMPNAME +"," +
C_COMPANY_COMP_URL+"," +
C_COMPANY_CALC_GPS_COORDS +"," +
C_COMPANY_SYSTEM_EXTERNAL_TIME_TOLERANCE +"," +
C_COMPANY_CARER_REPORTING +"," +
C_COMPANY_SHOW_DIRECTIONS+"," +
C_COMPANY_LONE_WORKER +"," +
C_COMPANY_SHOW_DELETE_BUTTON +"," +
C_COMPANY_CARER_MESSAGING +"," +
C_COMPANY_CANNOT_SIGN_IN_INTERVAL +"," +
C_FORMATTED_TAGS_ONLY+"," +
C_LOG_TO_SDCARD +"," +
C_SHOWGENERALNOTES+"," +
C_SHOWENTRYMETHOD +" ) SELECT " + C_ID_COMPANY_ID_OUTSIDE_APP_PURPOSES + "," +
C_COMPANY_ID_OUTSIDE_APP_PURPOSES +"," +
C_COMPANY_TAG_TOUCH_INTERVAL +"," +
C_COMPANY_SAVE_PASSWORD +"," +
C_COMPANY_ALLOW_CHANGE_USER +"," +
C_COMPANY_DISPLAY_ROTA_DETAILS +"," +
C_COMPANY_DISPLAY_CLIENT_PHONENUMBER +"," +
C_COMPANY_DISPLAY_CLIENTKEYSAFE +"," +
C_COMPANY_DISPLAY_DOUBLEUP_CARER +"," +
C_COMPANY_DISPLAY_CONTACTS+"," +
C_COMPANY_AUTOLOGOUT +"," +
C_COMPANY_DISPLAY_NOTES+"," +
C_COMPANY_DISPLAY_MEDS +"," +
C_COMPANY_ROTALOGOUT +"," +
C_COMPANY_DISPLAY_ACTUAL_TIME +"," +
C_COMPANY_QRCODEENABLED+"," +
C_COMPANY_MANUAL_INPUT +"," +
C_COMPANY_ROTAS_ONLY+"," +
C_COMPANY_COMPNAME +"," +
C_COMPANY_COMP_URL+"," +
C_COMPANY_CALC_GPS_COORDS +"," +
C_COMPANY_SYSTEM_EXTERNAL_TIME_TOLERANCE +"," +
C_COMPANY_CARER_REPORTING +"," +
C_COMPANY_SHOW_DIRECTIONS+"," +
C_COMPANY_LONE_WORKER +"," +
C_COMPANY_SHOW_DELETE_BUTTON +"," +
C_COMPANY_CARER_MESSAGING +"," +
C_COMPANY_CANNOT_SIGN_IN_INTERVAL +"," +
C_FORMATTED_TAGS_ONLY+"," +
C_LOG_TO_SDCARD +"," +
C_SHOWGENERALNOTES+"," +
C_SHOWENTRYMETHOD +" FROM Old"+TABLECOMPANYID);
db.execSQL("DROP TABLE Old"+TABLECOMPANYID);
Log.e(TAG, " SQLiteOpenHelper onUpgrade: finished copying old data to new companyoptions table");
this.onCreate(db);
}//end of onUpgrade
}
[EDIT1]
private class DBHelper extends SQLiteOpenHelper {
// database name and version number
public static final String DB_NAME = "carefreemobiledb.db";
public static final int DB_VERSION = 54;
// table names
public static final String TABLETRANSACTIONS = "transactions";
public static final String TABLECARER = "carer";
public static final String TABLETRANSACTIONSMAP = "transactionsmap";
public static final String TABLEPHONE = "phone";
public static final String TABLECOMPANYID = "companyid";
public static final String TABLEBACKGROUNDSERVICES = "backgroundservices";
public static final String TABLEMESSAGE = "message";
public static final String TABLEDUPLICATETRANSACTIONS = "tableduplicatetransactions";
public static final String TABLECACHEDROTA = "tablecachedrota";
public static final String TABLEUSERS = "users";
public DBHelper() {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.e(TAG, "SQLiteOpenHelper oncreate ");
String sqlToCreateTransactionsTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s INT, %s TEXT, %s TEXT, %s TEXT )",
TABLETRANSACTIONS, C_ID, C_TYPE, C_COMPANY_ID,
C_PERSON_ID, C_NAME, C_TAG_ID, C_STATUS,
C_TAG_SCAN_TIME, C_TAG_SENTSERVER_TIME,
C_TRANSACTIONS_LATITUDE, C_TRANSACTIONS_LONGITUDE,
C_DRIVER);
db.execSQL(sqlToCreateTransactionsTable);
Log.e(TAG, "oncreate " + sqlToCreateTransactionsTable);
String sqlToCreateCarerTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TABLECARER, C_ID_INDEX, C_CARER_ID, C_COMP_ID,
C_CARER_FIRSTNAME, C_CARER_LASTNAME, C_PASSWORD,
C_DATE_TIME);
db.execSQL(sqlToCreateCarerTable);
Log.e(TAG, "oncreate " + sqlToCreateCarerTable);
String sqlToCreateTransactionsmapTable = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TABLETRANSACTIONSMAP, C_ID, C_TYPE, C_COMPANY_ID,
C_PERSON_ID, C_NAME, C_TAG_ID, C_STATUS,
C_TAG_SCAN_TIME, C_LONGITUDE, C_LATITUDE);
db.execSQL(sqlToCreateTransactionsmapTable);
Log.e(TAG, "oncreate " + sqlToCreateTransactionsmapTable);
String sqlToCreatePhone = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLEPHONE, C_ID_PHONE, C_PHONE_NAME,
C_PHONE_NUMBER);
db.execSQL(sqlToCreatePhone);
Log.e(TAG, "oncreate " + sqlToCreatePhone);
String sqlToCreateUsers = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLEUSERS, C_ID_USERS, C_USER_NAME, C_USER_ID);
db.execSQL(sqlToCreateUsers);
Log.e(TAG, "oncreate " + sqlToCreateUsers);
String sqlToCreateMessage = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)",
TABLEMESSAGE, C_ID_MESSAGE, C_MESSAGE_GUID,
C_MESSAGE_TEXT, C_MESSAGE_CREATED_AT,
C_MESSAGE_SENDER, C_MESSAGE_REPLIED,
C_MESSAGE_SEEN, C_MESSAGE_DISPLAYED,
C_MESSAGE_REPLY_MESSAGE, C_MESSAGE_IS_STANDALONE);
db.execSQL(sqlToCreateMessage);
Log.e(TAG, "oncreate " + sqlToCreateMessage);
String sqlToCreateBackgroundServices = String.format(
"create table %s ( %s INTEGER primary key, %s TEXT)",
TABLEBACKGROUNDSERVICES, C_ID_BACKGROUNDSERVICES,
C_BACKGROUNDSERVICES_HAVE_RUN_ONCE);
db.execSQL(sqlToCreateBackgroundServices);
Log.e(TAG, "oncreate " + sqlToCreateBackgroundServices);
String sqlToCreateCompanyId = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT,"
+ " %s TEXT, %s TEXT, %s TEXT)", TABLECOMPANYID,
C_ID_COMPANY_ID_OUTSIDE_APP_PURPOSES,
C_COMPANY_ID_OUTSIDE_APP_PURPOSES,
C_COMPANY_TAG_TOUCH_INTERVAL,
C_COMPANY_SAVE_PASSWORD,
C_COMPANY_ALLOW_CHANGE_USER,
C_COMPANY_DISPLAY_ROTA_DETAILS,
C_COMPANY_DISPLAY_CLIENT_PHONENUMBER,
C_COMPANY_DISPLAY_CLIENTKEYSAFE,
C_COMPANY_DISPLAY_DOUBLEUP_CARER,
C_COMPANY_DISPLAY_CONTACTS, C_COMPANY_AUTOLOGOUT,
C_COMPANY_DISPLAY_NOTES, C_COMPANY_DISPLAY_MEDS,
C_COMPANY_ROTALOGOUT,
C_COMPANY_DISPLAY_ACTUAL_TIME,
C_COMPANY_QRCODEENABLED, C_COMPANY_MANUAL_INPUT,
C_COMPANY_ROTAS_ONLY, C_COMPANY_COMPNAME,
C_COMPANY_COMP_URL, C_COMPANY_CALC_GPS_COORDS,
C_COMPANY_SYSTEM_EXTERNAL_TIME_TOLERANCE,
C_COMPANY_CARER_REPORTING,
C_COMPANY_SHOW_DIRECTIONS, C_COMPANY_LONE_WORKER,
C_COMPANY_SHOW_DELETE_BUTTON,
C_COMPANY_CARER_MESSAGING,
C_COMPANY_CANNOT_SIGN_IN_INTERVAL,
C_FORMATTED_TAGS_ONLY, C_LOG_TO_SDCARD,
C_SHOWGENERALNOTES, C_SHOWENTRYMETHOD,
C_PROMPTFORDRIVER);
db.execSQL(sqlToCreateCompanyId);
Log.e(TAG, "oncreate " + sqlToCreateCompanyId);
String sqlToCreateDuplicateTransactions = String.format(
"create table %s ( %s INTEGER primary key, %s TEXT)",
TABLEDUPLICATETRANSACTIONS, C_ID_DUPLICATETRANSACTIONS,
C_DUPLICATETRANSACTIONS);
db.execSQL(sqlToCreateDuplicateTransactions);
Log.e(TAG, "oncreate " + sqlToCreateDuplicateTransactions);
String sqlToCreateCachedRota = String
.format("create table %s ( %s INTEGER primary key, %s TEXT, %s TEXT)",
TABLECACHEDROTA, C_ID_CACHED_ROTA,
C_CACHED_ROTA_DATE, C_CACHED_ROTA);
db.execSQL(sqlToCreateCachedRota);
Log.e(TAG, "oncreate " + sqlToCreateCachedRota);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "SQLiteOpenHelper onUpgrade ");
if (oldVersion < 54) {
// do upgrade from 52/53 to 54
//copy transaction data
db.execSQL("ALTER TABLE "+TABLETRANSACTIONS+" ADD " + C_DRIVER + " TEXT");
Log.e(TAG, "Just added driver column to Transactions table");
db.execSQL("ALTER TABLE "+TABLECOMPANYID+" ADD " + C_PROMPTFORDRIVER + " TEXT");
Log.e(TAG, "Just added promptfordriver column to companyid table");
}
}//end of onUpgrade
}
答案 0 :(得分:1)
我通常运行数据库脚本(下面有更多内容),但如果您严格遵守SQLiteOpenHelper
模式,那么最好用自己的方法包装每个版本,比如createVersion0
,upgradeVersion1
,updgradeVersion2
等。您的onCreate
将如下所示:
@Override
public void onCreate(SQLiteDatabase db) {
createVersion0( db );
upgradeVersion1( db );
upgradeVersion2( db );
....
}
你会相应地更改你onUpgrade
。
我通常运行数据库脚本,其中这些脚本在资产中,而onCreate和onUpgrade只调用applyScripts
方法以使数据库加速。关于这一点的好处是你可以使用ALTER TABLE .... ADD COLUMN ....
语句,这比删除和重新创建每个表要清晰得多,特别是如果你喜欢它们中的数据。
答案 1 :(得分:0)
我会创建一个额外的方法来创建新表,并从onUpgrade
和onCreate
调用它。
关于升级现有的:SQLite支持ALTER TABLE ... ADD COLUMN ...
!它不会使现有的表升级更容易吗?