return endPtr >= str ? endPtr : NULL; }/* * Connect to the datasource * * The connect string can have the following parts and they refer to * the values in the odbc.ini file * * DSN=<data source name> [mandatory] * HOST=<server host name> [optional - value of Host] * SVT=<database server type> [optional - value of ServerType] * DATABASE=<database path> [optional - value of Database] * OPTIONS=<db specific opts> [optional - value of Options] * UID=<user name> [optional - value of LastUser] * PWD=<password> [optional] * READONLY=<N|Y> [optional - value of ReadOnly] * FBS=<fetch buffer size> [optional - value of FetchBufferSize] * * Examples: * * HOST=star;SVT=Informix 5;UID=demo;PWD=demo;DATABASE=stores5 * * DSN=stores5_informix;PWD=demo */ int CHandleDB::DB_Connect(char *szDBName,char *szUID,char *szPWD) { SQLCHAR dsn[50]; SQLCHAR desc[255]; SWORD len1, len2; int status;#if (ODBCVER < 0x0300) if (SQLAllocEnv (&m_henv) != SQL_SUCCESS) return -1; if (SQLAllocConnect (m_henv, &m_hdbc) != SQL_SUCCESS) return -1; #else if (SQLAllocHandle (SQL_HANDLE_ENV, NULL, &m_henv) != SQL_SUCCESS) return -1; SQLSetEnvAttr(m_henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3, SQL_IS_UINTEGER); if(SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &m_hdbc) != SQL_SUCCESS) return -1; #endif /* * Either use the connect string provided on the command line or * ask for one. If an empty string or a ? is given, show a nice * list of options */ status = SQLConnect(m_hdbc, (UCHAR *)szDBName, SQL_NTS, (UCHAR *)szUID, SQL_NTS, (UCHAR *)szPWD, SQL_NTS);
SQLConnect函数建立与数据员的连接
SQLGetData操作记录结果 你可以查MSDN关于他们的使用
#ifndef DATABASE_H
#define DATABASE_H#include <sql.h>
#include <sqlext.h>class CHandleDB
{
public:
CHandleDB();
virtual ~CHandleDB(); SQLHENV m_henv;
SQLHDBC m_hdbc;
int m_DBconnected;
long m_lRecCount; char *rtrim (char *str);
SQLHSTMT DB_ExecQuery(char *szQuerySQL);
int DB_IsLastRec(SQLHSTMT hstmt);
char *DB_GetFieldInfo(SQLHSTMT hstmt,int nFieldIndex);
//long DB_GetRecCount(SQLHSTMT hstmt);
int DB_Connect(char *szDBName,char *szUID,char *PWD);
int DB_Disconnect (void);
int DB_Errors (SQLHSTMT hstmt);
int DB_ExecSQL(char * pSqlString);
};#endif
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <ctype.h>#include "handledb.h"CHandleDB::CHandleDB()
{
m_lRecCount = 0;
m_DBconnected = 0;
}
CHandleDB::~CHandleDB()
{
}char * CHandleDB::rtrim(char *str)
{
char *endPtr; if (str == NULL || *str == '\0')
return NULL;
for(endPtr=&str[strlen(str)-1];endPtr>=str && isspace(*endPtr);endPtr--);
endPtr[1] = 0;
return endPtr >= str ? endPtr : NULL;
}/*
* Connect to the datasource
*
* The connect string can have the following parts and they refer to
* the values in the odbc.ini file
*
* DSN=<data source name> [mandatory]
* HOST=<server host name> [optional - value of Host]
* SVT=<database server type> [optional - value of ServerType]
* DATABASE=<database path> [optional - value of Database]
* OPTIONS=<db specific opts> [optional - value of Options]
* UID=<user name> [optional - value of LastUser]
* PWD=<password> [optional]
* READONLY=<N|Y> [optional - value of ReadOnly]
* FBS=<fetch buffer size> [optional - value of FetchBufferSize]
*
* Examples:
*
* HOST=star;SVT=Informix 5;UID=demo;PWD=demo;DATABASE=stores5
*
* DSN=stores5_informix;PWD=demo
*/
int CHandleDB::DB_Connect(char *szDBName,char *szUID,char *szPWD)
{
SQLCHAR dsn[50];
SQLCHAR desc[255];
SWORD len1, len2;
int status;#if (ODBCVER < 0x0300)
if (SQLAllocEnv (&m_henv) != SQL_SUCCESS)
return -1; if (SQLAllocConnect (m_henv, &m_hdbc) != SQL_SUCCESS)
return -1;
#else
if (SQLAllocHandle (SQL_HANDLE_ENV, NULL, &m_henv) != SQL_SUCCESS)
return -1; SQLSetEnvAttr(m_henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,
SQL_IS_UINTEGER); if(SQLAllocHandle(SQL_HANDLE_DBC, m_henv, &m_hdbc) != SQL_SUCCESS)
return -1;
#endif /*
* Either use the connect string provided on the command line or
* ask for one. If an empty string or a ? is given, show a nice
* list of options
*/
status = SQLConnect(m_hdbc,
(UCHAR *)szDBName,
SQL_NTS,
(UCHAR *)szUID,
SQL_NTS,
(UCHAR *)szPWD,
SQL_NTS);
if(status != SQL_SUCCESS && status != SQL_SUCCESS_WITH_INFO)
{
SQLFreeHandle(SQL_HANDLE_ENV,m_henv);
return -1;
} SQLSetConnectOption(m_hdbc, SQL_OPT_TRACEFILE, (UDWORD) "\\SQL.LOG"); m_DBconnected = 1; return 0;
}
/*
* Disconnect from the database
*/
int CHandleDB::DB_Disconnect (void)
{
#if (ODBCVER < 0x0300) if (m_DBconnected)
SQLDisconnect (m_hdbc); if (m_hdbc)
SQLFreeConnect (m_hdbc); if (m_henv)
SQLFreeEnv (m_henv);
#else if (m_DBconnected)
SQLDisconnect (m_hdbc); if (m_hdbc)
SQLFreeHandle (SQL_HANDLE_DBC, m_hdbc); if (m_henv)
SQLFreeHandle (SQL_HANDLE_ENV, m_henv);
#endif m_DBconnected = 0; return 0;
}
/*
* Show all the error information that is available
*/
int CHandleDB::DB_Errors (SQLHSTMT hstmt)
{
unsigned char buf[250];
unsigned char sqlstate[15];
int daemon_proc = 0; /*
* Get statement errors
*/
while( SQLError(m_henv, m_hdbc, hstmt, sqlstate, NULL,
buf, sizeof(buf), NULL) == SQL_SUCCESS)
{
if(daemon_proc)
syslog(LOG_ERR, "%s, SQLSTATE=%s\n", buf, sqlstate);
else
fprintf (stderr, "%s, SQLSTATE=%s\n", buf, sqlstate);
} /*
* Get connection errors
*/
while( SQLError(m_henv, m_hdbc, SQL_NULL_HSTMT, sqlstate, NULL,
buf, sizeof(buf), NULL) == SQL_SUCCESS)
{
if(daemon_proc)
syslog(LOG_ERR, "%s, SQLSTATE=%s\n", buf, sqlstate);
else
fprintf (stderr, "%s, SQLSTATE=%s\n", buf, sqlstate);
} /*
* Get environmental errors
*/
while( SQLError(m_henv, SQL_NULL_HDBC, SQL_NULL_HSTMT, sqlstate, NULL,
buf, sizeof(buf), NULL) == SQL_SUCCESS)
{
if(daemon_proc)
syslog(LOG_ERR, "%s, SQLSTATE=%s\n", buf, sqlstate);
else
fprintf (stderr, "%s, SQLSTATE=%s\n", buf, sqlstate);
} return -1;
}int CHandleDB::DB_ExecSQL(char * pSqlString)
{
long ret;
SQLHSTMT hstmt; if( !pSqlString || !m_DBconnected )
return -1;#if (ODBCVER < 0x0300)
if( SQLAllocStmt(m_hdbc,&hstmt) != SQL_SUCCESS )
return -1;
#else
if (SQLAllocHandle (SQL_HANDLE_STMT, m_hdbc, &hstmt) != SQL_SUCCESS)
return -1;
#endif ret = SQLExecDirect(hstmt,(SQLCHAR *)pSqlString,SQL_NTS);
if( (ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO) )
{
syslog(LOG_ERR,"Error: SQLExecDirect");
return -1;
}
SQLFreeStmt(hstmt, SQL_CLOSE);
#if (ODBCVER < 0x0300)
if (hstmt)
SQLFreeStmt (hstmt, SQL_DROP);
#else
if (hstmt)
{
int sts;
sts = SQLCloseCursor (hstmt);
if (sts != SQL_ERROR)
DB_Errors (hstmt);
SQLFreeHandle (SQL_HANDLE_STMT, hstmt);
}
#endif return 0;
}SQLHSTMT CHandleDB::DB_ExecQuery(char *szQuerySQL)
{
long ret;
char szGetRecordInfo[50];
SQLHSTMT hstmt;
SDWORD colIndicator; if( !m_DBconnected )
{
printf("not connect ,please check database!\n");
return NULL;
}#if (ODBCVER < 0x0300)
if( SQLAllocStmt(m_hdbc, &hstmt) != SQL_SUCCESS )
return NULL;
#else
if (SQLAllocHandle (SQL_HANDLE_STMT, m_hdbc, &hstmt) != SQL_SUCCESS)
return NULL;
#endif ret = SQLExecDirect(hstmt,(SQLCHAR *)szQuerySQL,SQL_NTS);
if( (ret != SQL_SUCCESS) && (ret != SQL_SUCCESS_WITH_INFO) )
{
DB_Errors(hstmt);
return NULL;
}
else
{
return hstmt;
}
}int CHandleDB::DB_IsLastRec(SQLHSTMT hstmt)
{
long ret;
ret = SQLFetch(hstmt);
if( (ret == SQL_NO_DATA) || (ret == SQL_NO_DATA_FOUND) )
{
return 1;
}
if(ret != SQL_SUCCESS)
{
DB_Errors (hstmt);
return -1;
}
m_lRecCount ++;
return 0;
}char *CHandleDB::DB_GetFieldInfo(SQLHSTMT hstmt,int nFieldIndex)
{
SDWORD colIndicator;
static char szGetRecordInfo[256];
memset(szGetRecordInfo,'\0',sizeof(szGetRecordInfo));
if( SQLGetData(hstmt,
nFieldIndex,
SQL_CHAR,
szGetRecordInfo,
sizeof(szGetRecordInfo),
&colIndicator
) != SQL_SUCCESS)
{
DB_Errors (hstmt);
return NULL;
}
if( colIndicator != SQL_NULL_DATA)
{
rtrim(szGetRecordInfo);
}
return szGetRecordInfo;
}