Accessing sqlite3 database from multiple view controllers

Discussion in 'Mac Programming' started by binjaja, Nov 17, 2008.

  1. binjaja macrumors newbie

    Joined:
    Nov 13, 2008
    #1
    I read the SQLITE3 example and the AppDelegate is used to open the SQLite datbase and it is also used to initiate all database activity.

    However, in my application , I have multiple view controllers and depending on user input , all of the view controllers would like to modify or read the database.

    My question is...how do you share an sqlite3* object between view controllers? In the SQLITE3 example they declare

    sqlite3* database

    in the AppDelegate and they assign to "database" using a C function call and passing it "&database". Then , AppDelegate
    opens the database.

    So how do my other view controllers get access to this?If I have a different view controller, I thought that I could put code in the AwakeFromNib that could message the AppDelegate for the database pointer. Then, I could assign the result to a local sqlite3* database variable in my view controller .Would that work? Is that necessary? Am I missing something simple? How do you make database queries from multiple view controllers without having to use the AppDelegate as the central point of control?
     
  2. darkwing macrumors 65816

    Joined:
    Jan 6, 2004
    #2
    I would just put the sqlite3 database pointer in your app delegate. You can access it with:

    Code:
    YourAppDelegate *delegate = (YourAppDelegate*)[[UIApplication sharedApplication] delegate];
    Then you can access it as a property. Do all your database editing in the same thread to avoid thread safety issues.
     

Share This Page