1

我有一个在数据库中有几个表的应用程序。我要升级其中的 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

    }

.

[编辑1]

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

    }
4

2 回答 2

1

我通常会运行数据库脚本(更多内容见下文),但如果您严格遵守该SQLiteOpenHelper模式,那么您最好将每个版本包装在自己的方法中,例如 , ,createVersion0等。然后您将看起来像这样:upgradeVersion1updgradeVersion2onCreate

    @Override
    public void onCreate(SQLiteDatabase db) {
        createVersion0( db );
        upgradeVersion1( db );
        upgradeVersion2( db );
        ....
    }

你会onUpgrade相应地改变你。

我通常运行数据库脚本,这些脚本在资产中,而 onCreate 和 onUpgrade 只是调用一个applyScripts方法来使数据库加速。这样做的好处是您可以使用ALTER TABLE .... ADD COLUMN ....语句,这比删除和重新创建每个表要干净得多,特别是如果您喜欢其中的数据。

于 2013-10-30T14:16:52.863 回答
0

我将创建一个额外的方法来创建新表并从onUpgrade和调用它onCreate

关于升级现有的:SQLite 支持ALTER TABLE ... ADD COLUMN ...!它不会使现有的表升级更容易吗???

于 2013-10-30T14:14:19.810 回答