How to get data from installed DB and insert the extracted data in New DB in iOS - ios

I have a Database (used SQLite ) file that i copy in the documents while the applicaiotn is installed , now i have added one field in a Table and i want to install new DB in the Device. To achieve this i think i have to do Database Migration.
The issue i am facing is that I am not able to think the Code to get Whole Data from the installed Database and then insert that extracted data into New Database (while installation) , I have less experienced in iOS Programming so please Provide details from Scratch .
Thanks in Advance :)

This is not really an iOS issue, but a conceptional problem.
If you want to add new tables to the database, why do you copy an old version of the database rather than one that already contains the new tables?
As for the import of existing data, simply read out the old database and write it to the new database. This should be simple enough with the available SQLite API. Explaining the details would be beyond the scope of this question. (You will find enough info on these basic things online.)

Related

iOS migrate SQLite when upgrade the app? [duplicate]

I have some general questions about iphone app updates that involves sqlite db.
With the new update does the existing sqlite db get overwritten with a copy of the new one?
If the update doesn't involve any schema changes then the user should be able to reuse the existing database with their saved data, right? (if the existing database doesn't get overwritten from 1 above )
If there are some schema changes, what's the best way to transfer data from the old database into the new one? Can some one please give me guidelines and sample code?
Only files inside the app bundle are replaced. If the database file is in your app's Documents directory, it will not be replaced. (Note that if you change files inside your app bundle, the code signature will no longer be valid, and the app will not launch. So unless you are using a read-only database, it would have to be in the Documents directory.)
Yes.
What's best depends on the data. You're not going to find sample code for such a generic question. First, you need to detect that your app is running with an old DB version. Then you need to upgrade it.
To check versions:
You could use a different file name for the new schema. If Version2.db does not exist but Version1.db does, do an upgrade.
You could embed a schema version in your database. I have a table called metadata with a name and value column. I use that to store some general values, including a dataversion number. I check that number when I open the database, and if it is less than the current version, I do an upgrade.
Instead of creating a table, you could also use sqlite's built-in user_version pragma to check and store a version number.
You could check the table structure directly: look for the existence of a column or table.
To upgrade:
You could upgrade in place by using a series of SQL commands. You could even store a SQL file inside your app bundle as a resource and simply pass it along to sqlite3_exec to do all the work. (Do this inside a transaction, in case there is a problem!)
You could upgrade by copying data from one database file to a new one.
If your upgrade may run a long time (more than one second), you should display an upgrading screen, to explain to the user what is going on.
1) The database file isn't stored as part of the app bundle so no, it won't get automatically overwritten.
2) Yes - all their data will be saved. In fact, the database won't get touched at all by the update.
3) This is the tricky one - read this fantastically interesting document - especially the part on lightweight migration - if your schema changes are small and follow a certain set of rules, they will happen automatically and the user won't notice. however, if ther are major changes to the schema you will have to write your own migration code (that's in that links as well)
I've always managed to get away with running lightweight migrations myself - it's by far easier than doing it yourself.
What I do is that I create a working copy of the database in the Documents directory. The main copy comes with the bundle. When I update the app I then have the option to make a new copy over the working copy, or leave it.

How to fetch record from sqlite database that has been created automatically when implemented in coredata

I have app in that i am using coredata. And when i install the app three files are getting created in the document directory called “test.sqlite, test.sqlite-shm and test.sqlite-wal". And now in second app i copy the test.sqlite in the folder and i want to access the database and tables but i got error like this "no such table found". Is there any issue that i create the database with coredata? please help me.
You either need to:
send all of the sqlite* files that Core Data creates
export the content from the data store, format it and send that
Option 1 should work and be fast to implement, but it isn't very future proof. Option 2 will take longer but is a much better solution.

i have changed the DB of my application and if i ll upload to apple store then what will happen ?

I have an application on apple store , due to some issue i have added one field in the DB of my Application .
My Query is: When i change application's DB i have to delete the existing application from simulator and then re install the application on Simulator , but when some one install it from app store then Do my customer also have to delete the existing application and then install the new one or what will happen in this DB change case ??
Or do i have to try something in code ??
Thanks in Advance :)
You will have to do database migration. Database migration enabled existing users of your application to install updates without having to delete old version. Also there old datas will migrate to the new version.
Are you using core data? If you are using core data then you should read Apple's documentation on core data migration.
If your application is storing any user specific data, you should not force the user to delete the application.
When you upload the new version, it should have provision to upgrade the DB. You can do it programmatically. To identify if user has upgraded or not, you can add some custom key into your .plist file or by any other mean. Once identify either you can migrate your core data or you can migrate your existing sqlite3 db by update table query.

iOS App SQLite database structure change

I am updating an old iOS app which used sqlite database. I changed the database structure adding columns to existing tables. Now, I am testing it on my device. If I clear my old app from iPad and then run this new updated version on it, it is working fine. But if I have the old version installed on ipad already and test this updated version, it is somehow using the old database instead of the one updated. Can some one help me why it is doing this?
My guess and to try and make a simple answer for you is this. It's likely you updated the database in the project file - which means when you run it, your new db will exist in the bundle. files in the bundle cannot be updated, so its common practice to copy the database out of the bundle and store it somewhere in the ios sandbox. I usually use the documents directory to keep it simple.
Most likely what is happening is that when you run it over a pervious install, it see's that the file is already copied over to the device so it does not touch it, however on new installs, it probably sees the database is missing so it copies it there and that is why on new installs it works fine but existing ones it does not.
Look in the app delegate or your root view controller for code that checks for the existing database and copies the database over if needed on startup.
If you need to update the database on existing installs, you would need to force the copy.
Beware though if you have data in the existing database not to overwrite it if its important. If important data is stored there, you have to either do a little shell game of getting the data and importing into the new database, or maybe a simpler way, is to run the database schema modification commands on the existing database so it is the same.
again, beware and make a copy of the local database file before you run those commands, just in case.
best of luck
In iOS, a SQLLite database is really just a file. When you used the old app, it created the schema in the database file. When you load the new app, the data remains, untouched. If you want to use the new schema, you will have to detect the old schema and update the existing data. I believe that there are documented ways to deal with this. Bryanmac's question reference seems to be a good place to start.
When you install a new version of your app, iOS actually installs it in a new directory and then copies the contents of the documents folder from the older version to the one in the newer version. If you want to just use your new db, the best way is to have this db renamed or stored in a different directory inside your app's document store.
Here's a relevant article on updating An sqlite CoreData backing store on iOS:
http://www.musicalgeometry.com/?p=1736

Reverse-engineering Core Data db results in "Can't find model for source store" error

I have the task to re-engineer an iPhone app which makes use of Core Data to store some values. Unfortunately I do not have access to the original source code but I do have acces to old database files, copied directly from the device where the old version of the app is installed.
I have to create a new version of the app with some new functionality and I am trying to import the data from the Sqlite Db and migrate it to a new version.
I am already failing at the task to read the data from the old database. Though I can open the Sqlite file and such was able to exactly reproduce the data structure in my own datamodel, everytime I try to read the data, it fails with a
Can't find model for source store
error.
Ignoring the old data is not an option because there is important data stored there.
I googled for solutions and tried every recommended solution related to migrating data but it all fails. Maybe I can not use the Sqlite created by another app? Or so I overlook something in re-engineering the structure of the datamodel even when I used exactly the some field names and datatypes? Is there another way around this?
I could not solve the issue but I found a workaround. By using Sqlite directly, I was able to query the legacy data and import it into my newly created data model. A good starter point for using Sqlite is here:
http://www.techotopia.com/index.php/IOS_4_iPhone_Database_Implementation_using_SQLite

Resources