Android Question Can I modify a custom type, while it's already used in a database list?

creeper82

Member
I'm using a custom type that consists of keys like title, text, datetime, keywords etc. My simple "database" is just a list of hundreds of these.
The problem is that as for now I'm using their Unix times to identify them, and that was a big mistake, as due to how the app works there will be multiple of the same dates.
So naturally, I want to include the ID key in my custom type now.

The database is saved and loaded to/from file using RandomAccessFile library. How can I "migrate" the database to convert the old custom types to the new one? All the other keys are the same, I just want to add the "ID" key that I will pre-populate with increasing numbers while doing that. Will I need to create a second temporary type for that?

How will the app behave if I load up the RandomAccessFile, but the custom type has been now modified with an extra key? Will its' values be empty, or will it crash while loading the file into a list?

TL;DR I want to add a new key "ID" to my custom type, but the database (not sql or anything. pure list) list contains these types in the "old version", so without the newly added key in custom type definition. Will the list load from RandomAccessFile correctly and can I just iterate through it setting the new key to the values I want?
 
Solution
I assume that when you read the dataset that you'll have a List of objects of the old custom type?
If so, then loop through the items and copy each item (element by element) into the new custom type and add it to a new List.
Then save that new List.

If you want to preserve the old Type name, just do this twice, changing the names accordingly.

The whole process is a stand alone once off task.

William Lancee

Well-Known Member
Licensed User
Longtime User
I assume that when you read the dataset that you'll have a List of objects of the old custom type?
If so, then loop through the items and copy each item (element by element) into the new custom type and add it to a new List.
Then save that new List.

If you want to preserve the old Type name, just do this twice, changing the names accordingly.

The whole process is a stand alone once off task.
 
Last edited:
Upvote 1
Solution

creeper82

Member
Okay. thank you so much. So if I would copy them to the temporary new type name and would want to preserve the old type name I can't just change the name back in type declaration. I'll need to again create a third type and copy from the second to third, and finally save. Then of course remove the remaining temporary first and second.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
No.
Step 1 copy items from old type to temp type, save dataset
Step 2 read new dataset, copy items from temp type to old type name with revised structure, save - Done

If you need more guidance let me know.
 
Upvote 0

creeper82

Member
I'm gonna do it tomorrow. Thank you so much for the reply, I didn't think about the fact that I can just change the old type structure instead of making a third one. If something will go wrong I'll post it there
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I'm using a custom type that consists of keys like title, text, datetime, keywords etc. My simple "database" is just a list of hundreds of these.
Although the approach you are taking would work, an alternative to what you are trying to do, especially that you said you have hundreds of custom type elements in a list is to use SQLite database.
1. You create a database and a table that has ID as INTEGER PRIMARY KEY and the rest of the fields you want to have
example:
B4X:
CREATE TABLE IF NOT EXISTS  test (ID INTEGER PRIMARY KEY, name TEXT, age INTEGER)
2. You open the custom type list file and read it via B4XSerializator ( requires randomaccess lib)
3. You cycle through all the custom type records from the list and extract their components and insert into the table.
example:
B4X:
strQuery="INSERT INTO test VALUES(?, ?, ?)"
For i=0 To l.Size-1   'l is the original list
            Dim c As ct  'c is the custom type , in this case it has 2 elements: name and age
            c.Initialize
            c=l.Get(i)
            SQL.ExecNonQuery2(strQuery, Array As Object(Null, c.name, c.age))
 Next
4. That is all. You can leave the list file or delete it. , no temp files or other lists are needed. Pnce the data is in a table, it is a lot more practical to manipulate its data any way you want.
 
Upvote 1

creeper82

Member
4. That is all. You can leave the list file or delete it. , no temp files or other lists are needed. Pnce the data is in a table, it is a lot more practical to manipulate its data any way you want.
Maybe in future... Could file save size be better than RandomAccessFile saving the list object? I'm not even talking about the speed as I'm sure filtering results and searching by keywords will be handled by SQL very well. But I'd have to learn the SQL queries (pretty simple I guess, but the list works pretty good so I'm fine for now). Thanks so much for the suggestion and the code snippets! That's really nice to have for future, maybe for >1000entr. databases
 
Upvote 0
Top