1

DB に複数のテーブルを持つアプリがあります。両方のテーブルに追加のフィールドを追加しているので、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

0

新しいテーブルを作成するための追加のメソッドを作成し、それをonUpgradeandから呼び出しますonCreate

既存のもののアップグレードについて: SQLite はALTER TABLE ... ADD COLUMN ...! 既存のテーブルのアップグレードがはるかに簡単になると思いませんか???

于 2013-10-30T14:14:19.810 に答える