Thread: C++ and SQL

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    C++ and SQL

    Hey,

    I was curious if there was a way to use SQL in C++. I'm trying to make a program, but using the windows registry is just a pain to manage things. I was curious if there was a way to download some sql source and use that.

    For example

    Code:
    #include <iostream>
    #include "sql-essential.h"
    
    using namespace std;
    
    int main ( void )
    {
         SQLDB sdb; //the db (which would be defined in sql-essential, if something like this exists
         sdb.sqlFile ( "sqldata.sql" ); //connects to a file (this program doesn't need to run a SQL server)
         int result = sdb.query("SELECT * FROM `table` WHERE a='value'" ); //does query
         cout << result << endl; //shows result
         return 0;
    }
    Is there any sort of code like this out there? I know Call of Duty 4 uses some sort of database without having to worry about connecting to a server (I think).

    If there is any way of doing SQL in C++ without having to have MySQL running as a service or process in the background I'd be very appreciative.

    Thanks in advance,

    Guitarist809
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229

  3. #3
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Odbc

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are only looking to store configuration settings then embedding cyberfish's suggestion of SQLite would be good.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    SQLite is C, though, so you cannot work with it as in your example. However, there are C++ wrappers around it, I believe, that should make you happy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Awesome.

    SQLite looks like it'll work. I found it pretty funny when I was looking at the source - I think the code has more comments than actual code.

    Thanks for the help guys. Much appreciated.
    ~guitarist809~

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    I'm sorry for posting again, but I'm having a ton of trouble finding a C++ wrapper. They either need BOOST or they only work with MFC. This has been giving me a headache all day.

    Does anyone know of a good C++ wrapper for SQLite that does not need BOOST or MFC?

    Thanks,

    Guitarist809
    ~guitarist809~

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What's wrong with Boost?

  9. #9
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    I've spent hours trying to get it to work. No matter what I do, Visual Studio 2008 keeps giving me linker errors (I followed the BOOST tutorials, doing exactly what they told me to do). I gave up
    ~guitarist809~

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you check out the SQLite wiki on wrappers?

    That said, I never did find one that I liked, so I ended up writing my own wrapper that mainly provided RAII. Unfortunately, I kind of abandoned it as an open source project to be released because I could not figure out how to provide a good interface to define user defined SQL functions.

    Still, I'd say that it is quite easy to write your own wrapper if RAII is mainly want you want. It is even easier now that new functions were introduced to the SQLite C interface.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Man, I'm about to give up on this.

    I've tried almost every one of the C++ wrappers offered, half of them don't work off the bat (even onces with examples, missing include files or other stuff), and if I'm lucky enough to get one working (which I did), it doesn't work in MFC.

    This is a total nightmare. Never in my wildest dreams would I have ever figured that getting SQLite to work with C++/MFC would be this hard.

    This is probably more of a windows question, but I keep getting .def/.lib/.dll files with some examples, I'm having difficulty figuring out what do do with these files. Google isn't being much help for this sort of stuff.

    Has anybody actually had any success with SQLite and C++ and unicode?

    Thanks,

    Guitarist809
    ~guitarist809~

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To use .lib In Visual Studio? Properties -> Linker -> Input -> Type the names in the box (under additional dependencies).

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The .lib files ship with dll files which are libraries. Include the .lib files dependencies as tabstop mentions. Include the header file you ought to be getting and then you can use the functions/classes/stuff from the header file (the code is in the dll file).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Thank you so much. (+1 if I could)

    I can't believe how simple it was to get this working. I thought for sure I was doomed

    Thanks again for the help.
    ~guitarist809~

Popular pages Recent additions subscribe to a feed