OnUpgrade addition of new SQLite fields



  • The problem is: there is an Annex with the OBD already installed on different smartphones and there is now a need to add two more fields to the database. OnUpgrade's method doesn't work, when it comes to a new bud, it makes a mistake.

    (1) table mytab has no column named apnds

     @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            if (newVersion > oldVersion) {
                db.execSQL("ALTER TABLE mytab ADD COLUMN apnds, gprsds INTEGER DEFAULT 0");
            }
        }
    

    If you delete the app and set it up with a new version, it's all recorded, but I have to keep the old OBD records. Table code: (apnds and gprsds in the previous version of the table)

     private static final String DB_CREATE =
            "create table " + DB_TABLE + "(" +
                    COLUMN_ID + " integer primary key autoincrement, " +
                    COLUMN_NAME + " text, " +
                    COLUMN_TESTTIME + " text, " +
                    COLUMN_IP1 + " text, " +
                    COLUMN_SOCKET1 + " text, " +
                    COLUMN_IP2 + " text, " +
                    COLUMN_SOCKET2 + " text, " +
                    COLUMN_APN + " text, " +
                    COLUMN_GPRS + " text, " +
                    COLUMN_APN_DS + " text, " +
                    COLUMN_GPRS_DS + " text " +
                    ");";
    


  • The point is, there were no columns in the table. The reason is probably the error in the syntaxis of the request.

    SQLite ADD COLUMN Can only add one column. So if you'd like to add more, write a few requests. For example:

    ALTER TABLE mytab ADD COLUMN apnds TEXT;
    ALTER TABLE mytab ADD COLUMN gprsds INTEGER DEFAULT 0;
    

    Also, when the column is added, it is better to indicate the type of data.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2