I've seen a few posts on here talking about "databases" as if they were just files and using fstream to manipulate the file pointer, etc.

I'm familiar with SQL, so it would be nice to simply be able to execute sql command (as can be done from vb.net) such as:

SELECT * FROM table_name

I googled and found sqlapi++, is that the best way to go?

Thanks,

Dave

>>I've seen a few posts on here talking about "databases" as if they were just files

Yes, database is a very broad term that describes everything from a simple text file to the most comples SQL databases such as MySQL.

>>I googled and found sqlapi++, is that the best way to go?
It appears to be ok, but I have not used it myself. But I would say that it is much better than not using any classes and attempting to do it all from scratch.

Hi
I am using standard ODBC interface to connect to SQL databases from within c or c++ programs. Almost every sql database, e.g. oracle, ms sqlserver, sql anywhere etc, fully supports ODBC, and it runs on windows, mac and unix as well. Here http://www.ianywhere.com/developer/product_manuals/sqlanywhere/0902/en/html/dbpgen9/00000261.htm you will get some information about the database system and odbc interface I am working with.

The only drawback is that standard ODBC is not object-oriented but rather a simple call level interface. As already mentioned there are some o-o extentions like sqlapi++ shareware or QT-sql (my recommendation if you are looking for o-o odbc interface)

Here are some statements showing you how to access a sql table customer from sybase sql anywhere database using odbc v3:

//...
  #include "ntodbc.h"
  SQLHENV hEnv; SQLHDBC hDbc;
  SQLCHAR constring[] ="connection string depends on specific database";
  SQLHSTMT hStmt; SQLRETURN rc; SQLINTEGER idcust, clen; SQLCHAR custname[50];

  // Allocation of environment handle
  SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv );

  // Allocation of database handle
  SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hDbc );

  // connect to database
  SQLConnect(hDbc, constring);

  // Allocation of statement handle
  rc = SQLAllocHandle( SQL_HANDLE_STMT, hDbc, &hStmt);

  // SQL statement_________________= 1 =___= 2 = 
  SQLCHAR sqlStatement[] = "SELECT idcust, custname FROM customer ORDER BY 1 DESC";

  // direct execution without preparation
  rc = SQLExecDirect( hStmt, sqlStatement, SQL_NTS );

  // fetching all records from result set
  while((rc = SQLFetch(hStmt)) != SQL_NO_DATA)
   {
     // get values = 1 = and = 2 = of current record 
     SQLGetData(hStmt, 1, SQL_C_ULONG, &idcust, 0, 0); 
     SQLGetData(hStmt, 2, SQL_C_CHAR, custname, sizeof(custname), &clen);
     cout << setw(5) << left << idcust << setw(50) << left << custname<< endl;
   }
// ...

I myself wouldn't use comprehensive interfaces such as sqlapi++ or QT-sql (which best
fits to famous QT) for plain ODBC is a handy interface for daily sql programming tasks.

tesu

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.