How to add sqlite db to iphone simulator.

Discussion in 'Mac Programming' started by sujithkrishnan, May 15, 2008.

  1. macrumors 6502

    May 9, 2008
    Hi all..

    I tried a sample app and found (by watching memory during debugging that the app is looking for a books.sql databse in application Document directory.

    Line in app is as follows...

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    But the folder specified above is not visible in my PC so that i can put the books.sql file into that folder.

    That folder is created only after launching the simulator.

    So my question is that how to direct my iPhone app to a fixed filepath...

    Whether that file get ported to device if i put in some other folder and specify that path??

    I want read and write actions on that database..


    { I read that adding as a resource will give us only read permission, we cant write to DB }

    thanks in advance...
  2. macrumors newbie

    Mar 31, 2008

    Not true, I have included an SQLite database as a resource in one of my iPhone projects, and had no problems writing to it.

    Another alternative would be to programmatically create the SQLite database at a particular path on the iPhone when your application first launches, if it's not already there to begin with.
  3. macrumors newbie

    Oct 26, 2007
    Take a look at the SQLiteBooks sample app. Basically, on startup, check to see if your database file exists in the Documents directory (e.g. the path in the sample code you pasted below) and open that. If the file doesn't exist, then copy your SQLite DB file out of the Bundle to the Documents directory.
  4. macrumors newbie

    May 19, 2008

    I looked at the SQLiteBooks example and was wondering how to create the bookdb.sql file. It looks to me that the this is the file that creates the book database and has the statements that inserts the 3 records. How do you create the file and in the file do you have to include the sqlite3 my_database.sqlite statement as well?

  5. macrumors newbie

    Mar 31, 2008
    That file is the database. You can create such a file using an SQLite manager type of utility, or even create it programmatically using C if you know what you're doing.
  6. macrumors newbie

    May 19, 2008
    Thanks mysticwhiskey,

    Would you have any recommendations on a good SQLite manager utility.

  7. macrumors regular

    May 19, 2008
    The readme.txt file included with the SQLiteBooks project has instructions on how to create the database file via Terminal, and some basic instructions for creating tables.
  8. macrumors newbie

    Mar 31, 2008
    If you use Firefox, there's a free Firefox add-on to manage SQLite databases:

Share This Page