I have a database preloaded in SQLite: "BenedictusCoreData.sqlite".
I add it to the project in XCode.
I modify the BenedictusCoreData.xcdatamodeld to create the entities and attributes with the same name and type as the tables and columns of my sqlite preoloaded.
I run the app in the simulator without writing any additional code. I print the main bundle path and go to the sandbox of the application. And inside /Library/Application Support/ I find the three files: .sqlite .sqlite-shm .sqlite-wal
I open the .sqlite and obviously it is empty, however Xcode has created the tables and the columns with capital letters, adding "Z" to the tablenames and "Z" to the column names, appart from adding new tables "Z_METADATA" "Z_MODELCACHE", "Z_PRIMARYKEY".
At this point, I assume that it is not a good idea just to copy my sqlite from the bundle into the "Application Support" folder and rewrite the file, because the structure is totally different.
So I am lost about how to proceed in order to copy a sqlite preloaded database into CoreData the first time you open the app.
May be you can help me.
In the appDelegate you can change the default .xcdatamodel name to the one you added. Then xcode will load your preloaded data .sqlite file instead of the default one. You should search it up there's tutorials on this
Related
I am creating a Database called Reminder.sqlite via Sqlite Manager add-on in firefox, after creating I am importing it to my iOS Application, the problem is that records are saved in database table but when I run a Select query in Sqlite manager no records are displayed any suggestions why this is happening?
make sure you have add database file into project and remove database file from document directory.
because old file is exist in document director, your new file with same name cannot be overwrite old one.(according to approach, if we use in code)
It may help you.
My iOS app uses CoreData and some tables need to be filled with default values every first time the app is opened since CoreData device-dependent.
So I am using NSUserDefaults to check whether it is first time open. If it is first time, I fill tables (on CoreData) with the values which I have already created and formatted lines from the txt file by reading line by line and separating in a way.
And my question is, is it safe and fastest way to use txt file for such operation?
A better option would be to keep a "canned" sqlite file in your app bundle and check for the existence of the SQLite file in your documents directory. If the file does not exist, copy the canned data from the app bundle to your documents directory.
That will skip the entire parsing logic and will allow your application to launch faster.
Marcus' approach is also what I would recommend. But you can still keep using your parsing code during development in order to have a convenient way to create the seed SQLite file. When you ship your app, package the newest seed store as a bundle resource and disable / delete your parsing code.
How to store database from one sqlite file (which is in our bundle) to sqlite file (which is created while we using core data) while we launching application first time.
Should we copy each entity, each row in loop or Is there any other way to do this.
You can put sqlite file directly in your bundle, for example you can create sqlite file with SQLite manager in firefox. Then when start your app you can check if file exist in the document directory, if not you need to create it. This only the first time.
My app has a SQLite database. I don’t know how to explain, but sometimes all tables from the sqlite file gets deleted. It happen frequently. The .db file still exists but no tables.
The tables in an SQLite database (for a given path) will be removed only in these two situations:
The DROP TABLE command was executed against the database
The database file was replaced with a version that did not contain the tables.
Alternatively, the code may be creating a new database or the opening the wrong database file. If there are no tables then a new database/file was probably created. The "relative path" can play a role in where the SQLite file is opened from.
Find out which situation is occurring - and fix it.
I'm using a sqlite database in my iOS app. I have all its tables definitions in an .sqlite file I've placed in the "Supporting Files" group of the Xcode project, and in code I copy this file to "Documents" to be able to perform database operations. While developing, I've found that, when I need to add/remove a table or change its fields and I remove the .sqlite file from "Supporting Files" to add the new one, then I need to uninstall the app from the device or the simulator and build again to get the new database.
How could I make changes in database tables by replacing the .sqlite file without having to uninstall the app? When the app will be submitted to the App Store, will the users have to reinstall the app when a new update with changes in database is available?
EDIT. Is it possible to replace the database and to keep/copy the data the user had in the old one? Or will user loose all the stored data when downloading an app update from the Store where the .sqlite database is replaced, as if he were installing the app from scratch?
I've never submitted an app to the Store yet, I need some guidelines about how to handle app updates if I need to make changes in database tables when having such database in an .sqlite file. I'm not using Core Data.
Thanks
for it, you need to modify database by using programmatically. Means, if you want to create/add new table in database then you need to add code like Create Table.... In this case, you can't use predefine database in code.
But if you want to use database instead of writing code, then you need to rename your database and copy all old database data to new database programmatically.
Thanks