Thread: accessing an SQL database through C++

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    accessing an SQL database through C++

    I'd like to access an SQL database from within a Win32 application using C++. Does anyone have experience or suggestions on doing this?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I've used C to access MySQL. It was pretty straight forward. I can share my example if you want.

    In my day job, I work with a very large RDBMS, and C++ is one of the languages for which we provide an API - I just haven't gone there yet. All my work has traditionally been accessing the db via Assembler, and I've just gotten into Java as well.

    What RDBMS did you have in mind? Do you have any installed? Windows?
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Im going to use SQL, since I already have access to an SQL database system on my website and thus wont have to set anything up other than create another database ( I already have one but dont want it corrupted).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think what Dino was trying to say is "which SQL variant are you looking at", as they are very similar but not the same, so you usually have to know if it's MySQL (which there is easy access C code to use - and of course C code can be wrapped in a C++ wrapper if you want), MS Access (which I'm sure MS has access (no pun intended) functions for), or for example Oracle or DB2, which I'm sure the respective companies have interfaces for in C and/or C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use ODBC

    Connect using a DSN to hold site specific DB settings.

    MFC makes this a 2 min job.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Yes, I will unfortunately have to use MFC for this task, but I will probably wrap it in a DLL so I can access it from non-MFC code.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    ODBC is not a database, but merely a protocol (API) for accessing a database. See here: http://en.wikipedia.org/wiki/Odbc

    SQL is the language to tell the relational database system what you want. Different DBMSs support different flavors of SQL.

    Which database will you be using? Or, perhaps you don't know yet because you haven't picked one or don't know what is available.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    188

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by Dino View Post
    ODBC is not a database, but merely a protocol (API) for accessing a database. .
    Yes ODBC is used to access most types of DBs. Which is what the OP asked for.

    Quote Originally Posted by Dino View Post
    SQL is the language to tell the relational database system what you want. Different DBMSs support different flavors of SQL. .
    The differences in actual SQL between different versions is minor. In most cases it will not matter.

    Quote Originally Posted by Dino View Post
    Which database will you be using? Or, perhaps you don't know yet because you haven't picked one or don't know what is available.
    Or know it is irrelevant to the discussion, based on the design used/application requirements.



    EDIT: I think you are under-estimating our experience.
    Last edited by novacain; 11-20-2008 at 05:51 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by abachler View Post
    Yes, I will unfortunately have to use MFC for this task, but I will probably wrap it in a DLL so I can access it from non-MFC code.
    There are some issues with MFC, timestamps and Oracle.

    Watch MS SQL Servers auto-numbers (GUIDs) with ODBC consumer recordsets. Sometimes MSVC 'ignores' the field.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    From the req doc it says SQL Server 2000 or later, so im assuming MS products only.

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by abachler View Post
    From the req doc it says SQL Server 2000 or later, so im assuming MS products only.
    Which req docs, MFC or ODBC?

    AFAIK ODBC can be used with most DB types. I have used MFC ODBC only with SQL Server and Access.
    Can't remember if it was ODBC or ADO I used for Oracle in WIN32 C++.

    You can configure a DSN to use most DB drivers.
    The MFC CDatabase object can then use the DSN to connect, which should allow you to connect to most DB types.

    try;
    Add a DSN using Admin Tools to your DB, make sure to run the 'Test' to ensure it connects (firewalled).
    Start a new MSVC MFC DLL Project with DB support
    'Add Class' in MSVC and drill down to MFC->ODBC Consumer CRecordset class
    When you require a Data Source: try to connect to the DB using the DSN
    Derive a CRecordset from a table in the DB
    Check the members against the DB.

    If this works then, IMO, it is a proof of concept and you can move forward.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Using database in C++ with SQL. is it posible???
    By Lauris in forum C++ Programming
    Replies: 4
    Last Post: 04-18-2007, 09:31 AM
  3. Parsing a flat file to a SQl Server database?
    By Michael71 in forum C Programming
    Replies: 0
    Last Post: 01-16-2007, 12:45 PM
  4. accessing a database made in Access..
    By willc0de4food in forum Windows Programming
    Replies: 4
    Last Post: 10-10-2005, 07:40 PM
  5. SQL database Interaction
    By (TNT) in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2003, 03:52 AM