how to store files into sqlite - iphone

I m creating document files using filemanager which is text file.i want to store this files into sqlite tables?How can i store from iphone and what data types can be used?
Thank u..

Assuming your app is linked to a sqlite library, just create a database and then add tables and data. I'm not really clear where you're getting hung up.

Related

Pre Populate Core Data By Copying .sqlite File From Documents

I have read that this works, but I can't get it to. I want to pre populate Core Data. I ran my project so it would create the sqlite file. I copied that to my desktop and then prepopulated it using the command line, sqlite3. I ran queries to make sure it was populated correctly. So now I re-add it to my project. No matter what I do, though, it won't fetch the data.
I got it to work ONCE. I re-copied the sqlite file from my project's document folder after running it in the simulator. I then populated this new sqlite file. I put it back in my project and I got the contents to display.
I think the problem is the Z_METADATA table. This table has a Z_UUID attribute. I think the only time it worked was when the simulator's UUID and the sqlite's Z_UUID attribute were the same.
I went into ~/Library/Developer/Xcode/DerivedData and deleted the contents of the DerivedData folder. So now my simulator will have a different UUID when I run it. I did this and now the contents of the sqlite file will not display. This is why I think the Z_METADATA table is the problem. The Z_METADATA's UUID and my simulator's UUID are not the same so for some reason it can't use it?
All the articles about pre populating Core Data this way I found are old, around 2009, so I don't think this was a problem then. So can I use this method of copying the sqlite file, using the command line to pre populate it, then re-adding it to the project? Does anyone have a current way to prepopulate Core Data?
So the error is that my NSFetchedResultsController is returning a count of 0 objects. I got it to return all the data the one time I mentioned above.
According to the Core Data Programming Guide,
Important Although Core Data supports SQLite as a store type, the store format—like those of the other native Core Data stores—is private. You cannot create a SQLite database using native SQLite API and use it directly with Core Data (nor should you manipulate an existing Core Data SQLite store using native SQLite API). If you have an existing SQLite database, you need to import it into a Core Data store (see “Efficiently Importing Data”).
While I'm sure someone has reverse-engineered the Core Data sqlite format, Apple does not provide any documentation, and it is likely to change across across major OS releases.
Write a command-line app that uses Core Data to do your import.

iPhone - creating core data structures on OSX and populate them

OK, I know how to use core data and I know how to populate them from code on Xcode, but how do you guys manage to replicate the same structure on OSX and populate the tables, exporting the final sqlite file to Xcode?
What I mean is this: I want a way to see the same core data structures on OSX and have the ability to populate them with records from comma delimited files, for example, create all the modifications I need and then exporting the sqlite file and the xcdatamodeld file structure to Xcode.
And yes, I know a bunch of apps that enable me to read sqlite files and edit them, but this is not the same thing.
thanks
Sure there is. That is what I have already done. Just make sure you share the same ManagedObjectModel between these two apps. Than you can use your Mac App to populate the database, although I know that CoreData is not a database.
So create a usual CoreData App on the mac or iPhone and make sure you share the same ManagedObjectModel, so copy the file into the app. Then copy the database file from one app to the other and make sure Xcode includes them in the build process.

Trying to overwrite sqlite database in iPhone app update

Unlike most sqlite apps where developers don't want to overwrite data in a sqlite database, I'm trying to enable an app to overwrite all data with future updates. The sqlite database will have the exact same name and have the same tables, I just want to be able to update the data contained in the database without making users delete the app and reinstall it. Is there an easy method to do this?
Thanks in advance!
A SQLite database file is just a normal file, so no special steps are needed. Get the path or URL to the file, and use NSFileManager's -removeItemAtPath:error: or -removeItemAtURL:error:. Then create the new database the same way you created the old one.

Prefilling Core Data for a read-only application

i am working on an application that displays read-only data i am shipping.
it is more of a book.
It is easy with SQLite but i am not satisfied with the performance and trying to use Core Data.
The issue is with pre-filling Core Data is that it is a hard process.
My question is:
Is it possible to build an assistant iphone application (for me to use) which uses the same data model for pre-filling.
and then take the populated .xcdatamodel file and use it in my original application?
I hope this makes sense :)
Adham
I believe what you're asking is whether you can create a CoreData database upfront and copy it to the iPhone. Is that correct?
This article will help. Here's a quote:
I thus suggest the following five-step process:
Create your data in a comma-separated file, typically placing each row of data (an entity) in a row of the file and separating different columns (its attributes) by commas.
Write a standalone program and copy in your .xcdatamodel file from your main project.
Write code in your new program that parses your comma-separated file and inserts the information into a Core Data persistent store that should be identical to the persistent store in your main project.
Run the program in the Simulator
Copy your data from the Simulator's documents directory into your actual project's bundle.
It's possible, I've done it. I made a desktop application to read from a CSV file using the code here:
http://www.mac-developer-network.com/columns/coredata/may2009/
I just had to alter the way the CSV part worked, and change the model.
I copied and pasted my model from the model builder into the iPhone model. (Clicked on the "grid" area, selected all, copied)
Then I took the sqlite database the desktop app produced (found it in Application Support, in the folder for this application) and put it into the resources folder
I made some code to copy the sqlite into the documents folder on the iPhone (if it wasn't already there) at startup, in the applicationDidLaunch method. It's possible that having it in the resources folder is no good. Even though you're using the database as read only, Core Data may want to write something to it. Not sure about this though..
I used the sqlite file in the documents folder in my Core Data set up.
The desktop and iPhone Core Data sqlite file seem to be exactly the same format. You can transfer one sqlite file to another application (iPhone to iPhone too) as long as they have the same data model. In another application, I used NSXMLParser to create the Core Data sqlite file, then transferred it to another app, both on iPhone using the Simulator.
Yeah, your data source can be whatever you want it to be. The other suggestions are good ones. Create a managed object model (.xcdatamodel) identical to what you want to use in your app. Read in the data from your file, create a new instance of your managed object and populate it from the file. Then save, and dive into the bundle in the iPhone Simulator and copy it over. This has the added bonus of being in exactly the format you need, with all the helpful metadata. Copy your object model and your managed object classes and you're good to go.
Note, though, if you really intend for it to be read-only, and you're using it at install, it will be installed in your finished app's bundle (under Applications/{SIGNATURE}/Myapp.app). If you intend to edit this database or allow a user to save to it, it's a better idea to copy it to the Applications/{SIGNATURE}/Documents directory where your user database lives.

Provide Base Data for Core Data Application?

I'm working on a Core Data app (for iPhone 3.0, though I don't think that really makes a difference here) and it will need to ship with a "starter" database filled with data. With SQLite, I would just have the App copy the populated database from the bundle into the App's documents directory on first launch and then load that database - all the information would come along with it and we'd be ready to go. But with Core Data, I'm not really sure if I can just save the Persistent store to the App bundle and copy it before having Core Data start doing its thing. Will this cause any problems? There is quite a bit of initial data, so I don't want to package it in another format and have to parse through it.
Yes, you can copy over a pre-populated persistent store.
I created a Mac app that populates a store. It is copied into my bundle and at start, copied to the Docs directory. This works fine. I am told the Core Data Books example was developed the same way.
Please note this doesn't mean you can just copy over any old SQLite file. It has to be a Core Data persistent store, though I think you understand that based on your question.
Actually there is a trick: you must name the file you are going to copy over with an extension other than ".sqlite", ".bin" will do. Otherwise Xcode will change the contents of the file when it copies it into the app during the build phase and it won't load.