Add an SQLite database to an iPhone app - iphone

I am trying to learn SQLite, and I am building an iPhone app. But I would like to add an SQLite database to my building app. I have got three tutorials, but I am not clear on that code.
How can I add an SQLite database to my building app? What would sample code look like?

First of all you need to create your database.
From the command line create your db file.
sqlite3 mydb.db
Then create the tables within your database
CREATE TABLE tags (id int(5), name varchar(255), created_at datetime, updated_at datetime);
Repeat that for any tables that you want in your database.
Then you need to include the database file in your project. Add the existing database file to your project as you would any other existing file.
Next you will have to link in the framework to interact with the database. This can be found under you current iPhone SDK folder.
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/usr/lib/libsqlite3.0.dylib
Finally you have to include the header file sqlite3.h from
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/usr/include/sqlite3.h
It should now be possible to write code to access your sqlite database from within your iPhone application.

There is lots of information on the web.
Have you looked at the demo application? SQLite Book List This shows examples of common database functions under SQLite. This is effectively using the standard SQLite C APIs.
There are Objective C wrappers which may suite you more.
EntropyDB, SQLitePersistenceObjects and FMDB.
I found this Tutorial and this list of resources which may help.
Recently I've been using an ORM SQLite.net It is the way to go for me but then I'm developing in MonoTouch C#.
Tony

I'd also recommend looking at FMDB. It makes using SQLite slightly more Objective-C/Cocoa-like. It's not a full ORM wrapper or anything though; it just wraps the C API into something a bit more flavoursome.

http://sqlite.org/docs.html is a good place to start.
You might get more useful help if you are more specific about what you are trying to do, and what obstacles you are encountering.

If you use Firefox, there's this handy addon that you can use to manage and create an SQLite database.

you will have to link in the framework to interact with the database. This can be found under you current iPhone SDK folder.
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/usr/lib/libsqlite3.0.dylib

CoreData should help, but I can't find it anywhere in the list of importable Frameworks.
You could take a peek at the iPhone examples, especially the SQLite Book List example.

Related

How to bundle and read an SQLite database into Xcode 11.1?

I am a Swift newbie currently working on an IOS application, which needs to access an existing static database (size 400kb) of 1 table with about 3300 rows, and 26 columns. This database was initially a CSV file, converted into an SQLite file.
From forums I read online, it is to my understanding that SQLite queries are incredibly fast, and for that reason should be the preferred choice. I will only need to be reading the database and will not perform any writes.
I have found a number of old tutorials online which have utilized Objective-C or tutorials from several years ago which utilize swift 2 or 3. A newer solution I found was to use this SQLite wrapper: https://github.com/stephencelis/SQLite.swift. However, I am not entirely sure how to implement it. I was wondering if it was possible to read directly from my bundled database, or if I had to create a new table and copy from the bundled database.
It looks like SQLite.swift will get the job done for you. From the documentation on the page you linked it looks like you use Connection(pathToDB) to start using the library.
Since you're bundling the database and don't plan on modifying it you should be able to use Bundle.main.path(forResource:, ofType:) to get the path to your bundled database. If you want to modify it, you'll probably want to copy it to the documents directory and then reference that copy.
Another good answer on getting paths to various kinds of bundle resources if you need more help:
How to get path of image form Resource of main bundle

core data : phpmyadmin equivalent

In my app, I have a pretty huge datamodel and its .sqlite data file.
I would like to parse my records in the .sqlite file, and I'm wondering if there was a tool, like phpMyAdmin.
It would be an xCode tool, for example, or maybe a custom app downloadable on the web.
Does anyone know that kind of tool ?
There are any number of SQLite readers available, but SQLite Manager is popular:
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
i like the Core Data Editor, but it is quite expensive (23,99€)
Website:
http://christian-kienle.de/CoreDataEditor
Mac App Store:
http://itunes.apple.com/de/app/core-data-editor/id403025957?mt=12
the cool thing is, that core data editor is even able to generate (good and readable) code

Extract a table from call_history.db using C++ and sqlite

I'm developing an iphone app that retrieves the call log information and store it with the number of missed,incoming and received calls over that time. Here, since the call log is a .db file, i have to first access the call log details and extract it using C++ and sqlite.
Can anyone suggest me how to extract the table named "call" from call_history.db database file and store it in a text file?
I don't think that is allowed by Apple to access the call history database... If it is, which I seriously doubt, then you should be able to open it using sqlite3. There are plenty of tutorials available online how to do this but again, I think, if it is even possible, that your app will be rejected for this...
If you still want to try, the call_history.db file is probably a sqlite3 database.
Have a look at the documentation to see how to access it from your code. If you want to analyze the contents first to find out the schema, just use the command line application sqlite3.

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.

Is there a way to populate data via xcode?

I want to input data into a sqlite database so that core data can read from the file but I was wandering if there's a way of populating the sqlite data via xcode or do I have to spend lots of money on an sql manager of some kind like RazorSQL to do it?
I suppose you could do the following:
Create a CoreData-based project that uses the same data-model that you're going to use for your project. To be clear, make it a Mac project and not an iPhone project. Make sure that the project is using an SQLite store, and not something like XML or binary.
In Interface Builder, add a built-in CoreData Entity editor to your view and configure it to hook up with your model (most of this should be automatic!). Then, build and run this application, and you should be able to add as much data as you like. When you are done, simply find out where the persistent store was kept (usually somewhere like ~/Library/Application Support/MyAppName/) and copy that file for later use.
Then, you should be able to use that .sqlite file in any app that is made to work with the same data model. Good luck, and let me know how it goes!