timesten ODBC编程示例代码
相关链接:
http://space.itpub.net/81/viewspace-421428
《ODBC 2.0 Programmer’s Manual 》 Published by TimesTen Performance Software. Updated May 2000
http://www.oracle.com/technology/documentation/timesten_doc.html
Oracle TimesTen Documentation Library
下面的代码,在gcc中使用如下命令行参数:
>g++ -I/u02/Timesten/ttocs/include -L/u02/TimesTen/ttocs/lib -ltten -lgcc_s Noname1.cpp
/u02/Timesten/ttocs/ 数据库实例的路径
-ltten odbc 连接库
lgcc_s linux 下 gcc使用的动态连接库
//The following example constructs SQL statements within the application. The exampl
// comments include equivalent embedded SQL calls for illustrative purposes.
#include <sql.h>
#include <sqlext.h>
#include <string>
#include <iostream>
using namespace std;
#ifndef NULL
#define NULL 0
#endif
#define MAX_NAME_LEN 50
#define MAX_STMT_LEN 100
#define MAX_MSG_LNG 512
class TTODBCTest
{
private:
HENV henv;
HDBC hdbc;
HSTMT hstmt;
SDWORD id;
char name[MAX_NAME_LEN + 1];
char create[MAX_STMT_LEN];
char insert[MAX_STMT_LEN];
char select[MAX_STMT_LEN];
SQLLEN namelen;
RETCODE rc;
SQLCHAR szErrorMsg[MAX_MSG_LNG];
SQLSMALLINT pcbErrorMsg;
SQLCHAR szSqlState[MAX_MSG_LNG];
SQLINTEGER pfNativeError;
public:
int print_err( HSTMT stmt );
int TTODBCTest::example1( const char* server, const char * uid, const char * pwd);
};
int TTODBCTest::print_err( HSTMT stmt )
{
SQLError( this->henv, this->hdbc, stmt, szSqlState, &pfNativeError, szErrorMsg, MAX_MSG_LNG, &pcbErrorMsg );
cout << szErrorMsg << endl;
return 0;
}
int TTODBCTest::example1( const char* server, const char * uid, const char* pwd)
{
/* EXEC SQL CONNECT TO :server USER :uid USING :pwd; */
/* Allocate an environment handle. */
/* Allocate a connection handle. */
/* Connect to a data source. */
/* Allocate a statement handle. */
SQLAllocEnv(&henv);
SQLAllocConnect(henv, &hdbc);
rc = SQLConnect( hdbc, (SQLCHAR*)server, SQL_NTS, (SQLCHAR*)uid, SQL_NTS, (SQLCHAR*)pwd, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
return(print_err( SQL_NULL_HSTMT));
SQLAllocStmt(hdbc, &hstmt);
/* EXEC SQL CREATE TABLE NAMEID (ID integer, NAME varchar(50)); */
/* Execute the SQL statement. */
strcpy( (char*)create, "CREATE TABLE NAMEID (ID INTEGER, NAME VARCHAR(50))");
rc = SQLExecDirect(hstmt, (SQLCHAR*)create, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
return(print_err( hstmt));
/* EXEC SQL COMMIT WORK; */
/* Commit the table creation. */
/* Note that the default transaction mode for drivers that support */
/* SQLSetConnectOption is auto-commit and SQLTransact has no effect
SQLTransact(hdbc, SQL_COMMIT);
/* EXEC SQL INSERT INTO NAMEID VALUES ( :id, :name ); */
/* Show the use of the SQLPrepare/SQLExecute method: */
/* Prepare the insertion and bind parameters. */
/* Assign parameter values. */
/* Execute the insertion. */
strcpy( (char*)insert, "INSERT INTO NAMEID VALUES (?, ?)");
if (SQLPrepare(hstmt, (SQLCHAR*)insert, SQL_NTS) != SQL_SUCCESS)
return(print_err( hstmt));
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &id, 0, NULL);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, MAX_NAME_LEN, 0, name, 0, NULL);
id=500;
strcpy( (char*)name, "Babbage");
if (SQLExecute(hstmt) != SQL_SUCCESS)
return(print_err( hstmt));
/* EXEC SQL COMMIT WORK; */
/* Commit the insertion. */
SQLTransact(hdbc, SQL_COMMIT, SQL_COMMIT );
/* EXEC SQL DECLARE c1 CURSOR FOR SELECT ID, NAME FROM NAMEID; *
/* EXEC SQL OPEN c1; */
/* Show the use of the SQLExecDirect method. */
/* Execute the selection. */
/* Note that the application does not declare a cursor. */
strcpy( (char*)select, "SELECT ID, NAME FROM NAMEID");
if (SQLExecDirect(hstmt, (SQLCHAR*)select, SQL_NTS) != SQL_SUCCESS)
return(print_err( hstmt));
/* EXEC SQL FETCH c1 INTO :id, :name; */
/* Bind the columns of the result set with SQLBindCol. */
/* Fetch the first row. */
SQLBindCol(hstmt, 1, SQL_C_SLONG, &id, 0, NULL);
SQLBindCol(hstmt, 2, SQL_C_CHAR, name, (SQLLEN)sizeof(name), &namelen);
SQLFetch(hstmt);
/* EXEC SQL COMMIT WORK; */
/* Commit the transaction. */
SQLTransact(hdbc, SQL_COMMIT, SQL_COMMIT );
/* EXEC SQL CLOSE c1; */
/* Free the statement handle. */
SQLFreeStmt(hstmt, SQL_DROP);
/* EXEC SQL DISCONNECT; */
/* Disconnect from the data source. */
/* Free the connection handle. */
/* Free the environment handle. */
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return(0);
}
int main()
{
TTODBCTest tt;
tt.example1( "ttocs1", "unitele", "lemontea" );
return 0;
// comments include equivalent embedded SQL calls for illustrative purposes.
#include <sql.h>
#include <sqlext.h>
#include <string>
#include <iostream>
using namespace std;
#ifndef NULL
#define NULL 0
#endif
#define MAX_NAME_LEN 50
#define MAX_STMT_LEN 100
#define MAX_MSG_LNG 512
class TTODBCTest
{
private:
HENV henv;
HDBC hdbc;
HSTMT hstmt;
SDWORD id;
char name[MAX_NAME_LEN + 1];
char create[MAX_STMT_LEN];
char insert[MAX_STMT_LEN];
char select[MAX_STMT_LEN];
SQLLEN namelen;
RETCODE rc;
SQLCHAR szErrorMsg[MAX_MSG_LNG];
SQLSMALLINT pcbErrorMsg;
SQLCHAR szSqlState[MAX_MSG_LNG];
SQLINTEGER pfNativeError;
public:
int print_err( HSTMT stmt );
int TTODBCTest::example1( const char* server, const char * uid, const char * pwd);
};
int TTODBCTest::print_err( HSTMT stmt )
{
SQLError( this->henv, this->hdbc, stmt, szSqlState, &pfNativeError, szErrorMsg, MAX_MSG_LNG, &pcbErrorMsg );
cout << szErrorMsg << endl;
return 0;
}
int TTODBCTest::example1( const char* server, const char * uid, const char* pwd)
{
/* EXEC SQL CONNECT TO :server USER :uid USING :pwd; */
/* Allocate an environment handle. */
/* Allocate a connection handle. */
/* Connect to a data source. */
/* Allocate a statement handle. */
SQLAllocEnv(&henv);
SQLAllocConnect(henv, &hdbc);
rc = SQLConnect( hdbc, (SQLCHAR*)server, SQL_NTS, (SQLCHAR*)uid, SQL_NTS, (SQLCHAR*)pwd, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
return(print_err( SQL_NULL_HSTMT));
SQLAllocStmt(hdbc, &hstmt);
/* EXEC SQL CREATE TABLE NAMEID (ID integer, NAME varchar(50)); */
/* Execute the SQL statement. */
strcpy( (char*)create, "CREATE TABLE NAMEID (ID INTEGER, NAME VARCHAR(50))");
rc = SQLExecDirect(hstmt, (SQLCHAR*)create, SQL_NTS);
if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO)
return(print_err( hstmt));
/* EXEC SQL COMMIT WORK; */
/* Commit the table creation. */
/* Note that the default transaction mode for drivers that support */
/* SQLSetConnectOption is auto-commit and SQLTransact has no effect
SQLTransact(hdbc, SQL_COMMIT);
/* EXEC SQL INSERT INTO NAMEID VALUES ( :id, :name ); */
/* Show the use of the SQLPrepare/SQLExecute method: */
/* Prepare the insertion and bind parameters. */
/* Assign parameter values. */
/* Execute the insertion. */
strcpy( (char*)insert, "INSERT INTO NAMEID VALUES (?, ?)");
if (SQLPrepare(hstmt, (SQLCHAR*)insert, SQL_NTS) != SQL_SUCCESS)
return(print_err( hstmt));
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, &id, 0, NULL);
SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, MAX_NAME_LEN, 0, name, 0, NULL);
id=500;
strcpy( (char*)name, "Babbage");
if (SQLExecute(hstmt) != SQL_SUCCESS)
return(print_err( hstmt));
/* EXEC SQL COMMIT WORK; */
/* Commit the insertion. */
SQLTransact(hdbc, SQL_COMMIT, SQL_COMMIT );
/* EXEC SQL DECLARE c1 CURSOR FOR SELECT ID, NAME FROM NAMEID; *
/* EXEC SQL OPEN c1; */
/* Show the use of the SQLExecDirect method. */
/* Execute the selection. */
/* Note that the application does not declare a cursor. */
strcpy( (char*)select, "SELECT ID, NAME FROM NAMEID");
if (SQLExecDirect(hstmt, (SQLCHAR*)select, SQL_NTS) != SQL_SUCCESS)
return(print_err( hstmt));
/* EXEC SQL FETCH c1 INTO :id, :name; */
/* Bind the columns of the result set with SQLBindCol. */
/* Fetch the first row. */
SQLBindCol(hstmt, 1, SQL_C_SLONG, &id, 0, NULL);
SQLBindCol(hstmt, 2, SQL_C_CHAR, name, (SQLLEN)sizeof(name), &namelen);
SQLFetch(hstmt);
/* EXEC SQL COMMIT WORK; */
/* Commit the transaction. */
SQLTransact(hdbc, SQL_COMMIT, SQL_COMMIT );
/* EXEC SQL CLOSE c1; */
/* Free the statement handle. */
SQLFreeStmt(hstmt, SQL_DROP);
/* EXEC SQL DISCONNECT; */
/* Disconnect from the data source. */
/* Free the connection handle. */
/* Free the environment handle. */
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return(0);
}
int main()
{
TTODBCTest tt;
tt.example1( "ttocs1", "unitele", "lemontea" );
return 0;
}
范晨鹏
------------------
软件是一种态度
成功是一种习惯