sqlite_open(NSString,db) ??

Discussion in 'Mac Programming' started by Fritzables, Oct 15, 2011.

  1. Fritzables macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #1
    Hi All,

    I was hoping I could create/open a SQLite database using the file path in a NSString, but it appears this is illegal.

    Code:
    rc = sqlite3_open(fullPath,&db);
    Does this mean I have to convert the NSString to a char?
    If so, how is this best achieved?

    Pete
     
  2. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #2
    The sqlite API is in pure C, so yes you will need to use char* instead of NSString. There are a bunch of methods in NSString that will give you a C string. Look at the documentation.
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    Note that the sqlite C functions expect all strings to be encoded in UTF-8.
     
  4. Fritzables thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #4
    Thanks Guys,

    Got it worked out.... thanks for the assistance.

    Something weird happening though.... I am not a full bottle on Unix as yet but progressing slowly.

    What I have asked the app do after it loads is to create a database in the current directory, for example: /users/Fritzables/Development/Projects/

    When I run the app within XCode by using the Build and Run the database is created where I have asked it to, but, if I was to run the app directly from the Debug directory the database ends up in the root "/".

    Why does it do this?

    Pete
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Finder (or more specifically Launch Services) does not set the current working directory. The concept of the current working directory is not a useful one under Mac OS X, unless you change the working directory in your code.
     
  6. Fritzables thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
    #6
    G'Day Jim,

    I have created a variable that I place the applications current directory - well I thought so - but it ain't working.

    So, if I wanted to keep the database with the app regardless of where the app is placed - what are the options??

    Pete
     
  7. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #7
    Look at NSBundle. Your app (the .app directory containing your executable, etc.) is the main bundle.

    You want to use - pathForResource:ofType:inDirectory:. Note is this is the object method and not the class method. You don't want to use the class method.
     
  8. Fritzables thread starter macrumors regular

    Joined:
    May 6, 2011
    Location:
    Brisbane AUSTRALIA
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    You can't necessarily create a database with the app. Open, yes. Create, not necessarily.

    That's because the permissions of an installed app may forbid writing by the current user.

    If you're now thinking about allowing writing for all, don't. Public-write presents a security risk. It means any user can modify an app, altering it in ways that other users may find hostile.

    Read the reference docs for NSBundle, and its Companion guide docs, and learn about the directories where Mac OS X wants you to put app-specific things, user-specific things, etc.
     

Share This Page