Home > Net >  How do I create a new table (which should have a LocalDate variable) by migration?
How do I create a new table (which should have a LocalDate variable) by migration?

Time:10-21

That is my current dao

@PrimaryKey(autoGenerate = true)
    val id: Int,
    val name: String,
    val date: LocalDate,
    val amount: Int,
    val uri: String,
    val tag: String,
    val toList: Boolean,
    val inUse: Boolean,
    val listValue: Int

now I have the problem that in a previous version of that dao there is a variable in that table that I now want to remove.

I found a 4 step guid:

1.) create new table 2.) insert from the old table 3.) drop the old table 4.) alter new table name back to old table name

that's fine but my problem is that I have a variable with a LocalDate which uses a DateTypeConverter to function properly.

How do I insert that LocalDate into the new table? I just know of TEXT and INTEGER

CodePudding user response:

Step 2 use the SupportSQliteDatabase's execSQL method to execeute a query based upon the SQL

INSERT INTO <the_table> SELECT <the_columns> FROM <the_old_table>;

Where:-

  • anything enclosed within <> needs to be altered accordingly as per:-
    • <the_table> should be replaced with the new table name.
    • <the_columns> should be replaced with the column names, separated by commas, LESS THE DROPPED COLUMN NAME
    • <the_old_table> should be replaced with the old/original table name.

Note that a variable name will be the same as the variable name.

The above will copy the values, whatever they are, as stored in the database, from the old to the new table.

The TypeConverters are only used to convert the data to or from the respective object (LocalDate in your case) when storing or retrieving the stored data.

  • A type converter should consist of two functions:-
    • 1 to convert the object to a type that can be stored in an SQLite database (SQLite is a universal database that has no concept of a programming languages objects). The SQLite types being

      • INTEGER (not necessarily a Kotlin Int, could be a Long, Byte even a Boolean ....).
      • TEXT (a Kotlin String ....)
      • REAL (Kolin Double, Float ....)
      • BLOB (Kotlin ByteArray ....)
      • NULL
    • 2 to convert the stored type into the object when retrieving data from the database. As such it is no issue at all for the INSERT INTO table SELECT ....; to copy the existing data from one table to another irrespective of Room's handling of the data when it stores and retrieves the data.

    • The result being that the data is stored in the database as either one of the 5 types. As such it is no is

If the "current dao" (it is not a dao, it is an entity that should be annotated with @Entity, which equates to a table) is after the removal of the dropped variable then you would use:-

INSERT INTO <the_new_table> SELECT id,name,date,amount,uri,tag,toList,inUse,listValue FROM <the_old_table>;

You may wish to refer to 2. INSERT INTO table SELECT ...;

CodePudding user response:

If you want to add data into date column of old table data.



        fun updateData() {
        val list = dao.getAllData()
        list.forEach {
            //update data
        }
        dao.saveData(list)
    }

  • Related