Database programming in Cocoa

Discussion in 'Mac Programming' started by MacRohde, Jun 17, 2007.

  1. MacRohde macrumors regular

    MacRohde

    Joined:
    Jun 1, 2004
    Location:
    Copenhagen, Denmark
    #1
    I'm trying to get started in Cocoa/Objectiv-C (I program in C#/.NET at my day job) and I need to access some databases, but I can't seem to find any info on how to use Cocoa for data access.

    I've browsed the docs for Core Data but at the moment that's overkill, also the fact that I can't use MySQL is a no go, so it seems I have to use the C API that comes with MySQL - is that so?
     
  2. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #2
    In one word. Yes.

    In more words, you need to write a wrapper around the MySQL C API. It isn't difficult. All you will need is a basic class that opens a connection, closes a connection, executes a query as both a proper query with a result batch and as a simple update, delete or insert.
     
  3. aaronbrethorst macrumors member

    Joined:
    Feb 1, 2007
    Location:
    Seattle, WA
    #3
    You might want to check out the MySQL-Cocoa framework, although I've never actually tried using it, so your MMV. Good luck :)
     
  4. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #4
    Ha ha.

    That's so old, it isn't even being maintained anymore.
     
  5. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    You can use CoreData with SQL, and its really easy to program.
     
  6. MacRohde thread starter macrumors regular

    MacRohde

    Joined:
    Jun 1, 2004
    Location:
    Copenhagen, Denmark
  7. DaveGee macrumors 6502a

    Joined:
    Jul 25, 2001
    #7
    Yea but isn't that limited to SQLite? Last I head if you need to connect to remote databases and lets face it many/most/all true DB developers do... CD would be a dead end...

    So, sure if you want to take advantage of SQLite (no network connections) in a standalone app CD is great but after that it falls down quite rapidly... I sure hope this changes with Leopard!

    Dave
     
  8. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    It seems you are correct. It's local only for now, but remote (hopefully) is coming later, good that'll be useful for my application then!
     
  9. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #9
    This sounds like the best idea so far. Complicated, yes, but if you're using MySQL, you're not going to settle for SQLite.
     
  10. MacRohde thread starter macrumors regular

    MacRohde

    Joined:
    Jun 1, 2004
    Location:
    Copenhagen, Denmark
    #10
    Yes, and it might even be fun to do such a wrapepr - although it does take some time. Core Data seems nice and it would be pretty great if remote acces is added to the framework.
     
  11. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #11
    See, that's a big if.

    I'm not very familiar with SQLite, since I work as part of an in-house dev team where we have certain luxuries (such as our own servers) so we like to use MySQL so that we don't have to worry about regular users mucking around with the database.

    My general feeling is that you're opening a whole can of worms when you basically give Core Data the abilitiy to communicate with remote hosts. I imagine that since Core Data is part of the system, and there is the possibility that there are plenty of Carbon developers working on it, I don't really know if I'd want to give Core Data the ability to listen and transmit over the network, as a "standard feature" that is turned on when most people won't use it. Too much can go wrong, you know?

    I've looked over the Cocoa-MySQL and worst case, you could probably take a look at what they've done and maybe clean up their shop a bit, with whatever you end up doing, then submit it back to them. I'm sure they wouldn't mind.

    Good luck, either way. I know one of these days I'm going to start learning Cocoa, it's just that I have to wait for the job to get enough OS X machines to justify the time.
     
  12. MongoTheGeek macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #12
    I was so hoping to see that added to CoreData at WWDC. Its not like they don't ship MySQL with OSX anyway.
     
  13. garethlewis2 macrumors 6502

    Joined:
    Dec 6, 2006
    #13
    Even if they did add this type of access to CoreData, you wouldn't want to use unless you were writing the noddiest of programs. For anything remotely complex, you would want all the database access controlled by a single server object. All screens, controllers, etc would simply ask this server to execute a query on the database, and this server would handle the task of processing the results. It is also a better design as you could easily scale the application to run across several machines using NSDistributedObject, CoreData unless it becomes an actual service that can run from remote machines rather than locally is going to limit you to writing an application that can only run on a single machine, with multiple processors mind-you, but it is very very limiting.
     
  14. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #14
    Apparently custom data stores will be allowed in 10.5, but I really don't know what is expected in terms of what the data store provides to CoreData. If it lets you grab the fetch predicates and translate them to pass onto SQL, it would probably work just fine. If it requires that it handles fetches above you... then you are still right.
     

Share This Page