A Simple and Smart ODBC Wrapper Library

Environment:
  • Microsoft Visual C++ 6.0/SP4
  • ODBC 3.0 or above
  • Microsoft Windows 95...XP

    Direct usage of ODBC is very complex. Anybody who tries to work with ODBC can say it. Also, ODBC is a function-level interface. It is a good universal interface, but all structured C++ applications look very horrible when plain ODBC calls are used.

    I want to offer a little library, which:

    • Wraps access to ODBC into a set of classes
    • Wraps processing of ODBC and usage errors into an exception mechanism
    • Doesn't use any external libraries, including MFC and ATL
    • Doesn't require any special conditions to use it
    • Automates column binding operations
    • Can automate (for MSSQL server) binding of parameters
    • Can provide transparent work with LOB columns/parameters without limitation of their size
    • Has a Help file, even if this Help file is simple
    • Is freeware, open-source library
    • Supports powerful local-side cursors with indexing and modifying of cursor(insert, update, delete) are supported

    I sought such a library for some years, but I didn't find a library that conforms to all these conditions. Because I have used ODBC in many projects, some classes were developed some time ago. Now I have refined these classes. And I hope that these classes can help somebody.

    Downloads

    Download source - ~140 Kb
    Contains sources of library and Help files.

    Download demo project - ~70 Kb
    Contains project with a demonstration of the library usage.



  • Comments

    • 64bit or Windows 7 issue

      Posted by georgicus.fruticis on 01/28/2010 03:25am

      Hey Nick, we adapted your code a long time ago to access a MSAccess database. Now we are forced to switch to 64Bit Windows XP/7. For unknown reasons the execution of prepared statements with binary parameters don't set the binary values. ODBC gives me no error so I'm stuck. Any hint will be welcome. Greetings, Georgicus

      Reply
    • Simple Example

      Posted by auron on 02/07/2006 12:05pm

      Great library. If you are a novice to ODBC/Database programming it might throw you off at first if you compare it to other libraries. Here are a couple examples to get you started:
      
      I used MySQL to make a simple table called test with only two columns. You need to bind the columns manually first. 
      
      CGOdbcConnect cCon;
      cCon.connect("database", "username", "password");
      CGOdbcStmt *pCur;
      pCur = cCon.createStatement();
      
      Cur->prepare("insert into test values (?,?)");
      pCur->setParamNumber(2);
      pCur->bindParam(0, SQL_INTEGER, SQL_C_LONG, 4, 0, 0);
      pCur->bindParam(1, SQL_CHAR, SQL_C_CHAR, 33, 32, 0);
      
      pCur->setInt(0,123);
      pCur->setChar(1,"it worked!");
      pCur->execute();
      cCon.freeStatement(pCur);
      cCon.close();
      
      say you want to do a simple SELECT from a database you can just make a SQL statement, autobind, and get your values:
      
      CGOdbcConnect cCon;
      cCon.connect("database", "user", "password");
      CGOdbcStmt *pCur;
      pCur = cCon.createStatement();
      pCur->execute("SELECT * FROM articles");
      pCur->bindAuto()
      pCur->first();
      htmlPage = pCur->getChar("title");
      htmlPage += pCur->getChar("body");
      cCon.freeStatement(pCur);
      cCon.close();
      
      This library implements SQLPutData so you can post large amounts of data to your database. Understanding ODBC programming is a pain, but this code is cleanly written and using ibm and microsoft help articles you can use and extend these classes.
      
      I couldn't find any other resource that inmplements SQLPutData in a way that could be used immediately, but understood later. 
      
      Thanks again.

      • How to choose a MS Access file from such an example?

        Posted by founderideas01 on 03/23/2008 04:24pm

        Hi, Auron, I would like to choose a MS ACCess database from such an example, how to do it? In ODBC Microsoft access setup, in "User DSN" tab, if I connect my MS access file with Data Source name "MS Access 97 Database", I can run your example ( adapted with my case): int _tmain(int argc, _TCHAR* argv[]) { CGOdbcConnect cCon; cCon.connect("MS Access 97 database", "Admin", ""); CGOdbcStmt *pCur; pCur = cCon.createStatement(); pCur->execute("select * from Table1"); pCur->bindAuto(); pCur->first(); const char * htmlPage = pCur->getChar("Symbol"); const char * htmlPage1 = pCur->getChar("Event"); cCon.freeStatement(pCur); cCon.close(); return 0; } But, how can I choose my Access database file, from these lines of code instead of manually with the ODBC Access setup? Thanks a lot. Founder

        Reply
      Reply
    • hWnd in CGOdbcConnect::connect(const char *szStr, HWND hWnd) goes nowhere

      Posted by AngusEm on 07/19/2005 08:31pm

      It's a pity that this project isn't better documented, since it's much simpler to use that any other free ODBC API out there. There's also a problem w/the CGOdbcConnect::connect() when the driver needs to open a dialogue box w/the user: the hWnd is not used, and 0 is used in its place. I corrected the problem by changing: gsodbc_src/connect.cpp (line 76) rc = SQLDriverConnect(m_hConn, 0, (unsigned char *)szStr, SQL_NTS, to rc = SQLDriverConnect(m_hConn, hWnd, (unsigned char *)szStr, SQL_NTS, I'm not sure why that 0 is there. It looks deliberate, but my fix hasn't turned up any problems for me yet.

      Reply
    • Memory leaks???

      Posted by Legacy on 01/14/2004 12:00am

      Originally posted by: Jarle Thu

      Hi,
      Has anyone else had any problems with memory leaks using gsodbc?
      I'm no expert c++ programmer (which is why I use this library :-) but something is making my memory usage go sky high :(
      Just to check it I changed the demo app to do just logon/logoff in a loop.. this increases memory usage several 100k's for each pass..
      I've tried this with different odbc drivers and databases(ingres, MySQL)..

      Also in my "real" application it doesn't seems like memory is freed when I call
      cCon->freeStatement(pCur);
      after doing a DB querey, it only seems to free 4k...

      Anyone else had this problem or knows a workaround??

      Any help appreciated!

      Regards
      Jarle

      Reply
    • Excellent, the most well structured example I've seen in a long time.

      Posted by Legacy on 11/12/2003 12:00am

      Originally posted by: Jake

      I have used this library for some time now and I love it. It was wonderful to say goodbye to ADO as attempting to figure out what MS screwed up on was a nightmare.

      I give this code 5 *'s

      Reply
    • Spectacular

      Posted by Legacy on 08/11/2003 12:00am

      Originally posted by: Adrian

      Thanks

      Reply
    • How to insert data to BLOB field..?

      Posted by Legacy on 02/27/2003 12:00am

      Originally posted by: Keun


      Hi, all

      I try to insert data at BLOB field

      ora-12571 error

      Reply
    • Updating with cursors

      Posted by Legacy on 02/24/2003 12:00am

      Originally posted by: Levi Browde

      Updates using the cursor are not saving to the database. When I run the following code:
      
      

      <i>
      CGCursor * cCursor = new CGCursor( pCur);
      ...
      cCursor->go( j );
      ...
      char str[256];
      ...
      cCursor->setChar( "acct_name" , str);
      cCursor->update();
      </i>

      After the "setChar," a subsequent "getChar" indicates that the new value in "str" took, but the change is not committed to the database.

      I am able to update this field using the same ODBC Source via a query tool.

      Am I missing something obvious?

      Reply
    • Alias problem

      Posted by Legacy on 02/12/2003 12:00am

      Originally posted by: Lechuck

      I've got problem when I'm using alias in sql like :

      select value*10 as v from tablename;

      Error:=6357100
      Sql state:=00000

      Any idea how to solve this problem?

      Reply
    • Static linking?

      Posted by Legacy on 02/05/2003 12:00am

      Originally posted by: Stefan

      Is it possible to link this library into my exe? If yes, how to do it?

      Reply
    • Loading, Please Wait ...

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

    • Mobile devices, social business apps, and business analytics are converging with the Cloud to create the most substantial changes in technology since the Internet revolution. Businesses have to change the way they think and operate, and with rising budgets for technology, they need someone to provide the services that will keep them competitive in this environment. Learn more about the important technology trends you need to stay on top of to ensure your business doesn't get left behind.

    Most Popular Programming Stories

    More for Developers

    Latest Developer Headlines

    RSS Feeds