为什么不能与动态数据库连接
展开全部
//动态库和普通进程调用数据库方式没有任何区别,以下的程序兼容WINDOWS ODBC数据库连接和LINUX MYSQL连接,是跨平台的。
#ifdef WIN32
#include <windows.h>
#include <odbcinst.h>
#include <sqlext.h>
#else
#include <mysql.h>
#include <unistd.h>
#define SQLHANDLE static MYSQL
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
SQLHANDLE hDBEnv, hDBC;
int DB_Open(char * dbcn, char * usr, char * pwd)
{
int r;
#ifdef WIN32
r = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hDBEnv);
if(r) return 0;
r = SQLSetEnvAttr(hDBEnv,SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3,0);
if(r) return 0;
r = SQLAllocHandle(SQL_HANDLE_DBC, hDBEnv, &hDBC);
if(r) return 0;
r = SQLConnect(hDBC,
(unsigned char *)dbcn, strlen(dbcn),
(unsigned char *)usr, strlen(usr),
(unsigned char *)pwd, strlen(pwd));
return r==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO;
#else
mysql_init(&hDBC);
MYSQL * rx = mysql_real_connect(
&hDBC, dbcn, usr, pwd, NULL, 0, NULL, 0);
if(!rx) return 0;
return 1;
#endif
}//end DB_Open
int DB_Exec(char * sql)
{
#ifdef WIN32
SQLHANDLE hStatement = NULL;
SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStatement);
SQLExecDirect(hStatement,(unsigned char *)sql, strlen(sql));
SQLCloseCursor(hStatement);
SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
hStatement = NULL;
#else
mysql_real_query (&hDBC, sql, strlen(sql));
#endif
return 1;
}//end DB_Exec
int DB_Close(void)
{
#ifdef WIN32
SQLDisconnect(hDBC);
SQLFreeHandle(SQL_HANDLE_DBC, hDBC);
SQLFreeHandle(SQL_HANDLE_ENV, hDBEnv);
#else
mysql_close(&hDBC);
#endif
return 1;
}//DB_Close()
#ifndef WIN32
typedef struct tagMySQLRecordset {
MYSQL_RES * hRecord;
void * * row;
int * size;
int cols;
} MYSQLRecordset;
#ifdef WIN32
#include <windows.h>
#include <odbcinst.h>
#include <sqlext.h>
#else
#include <mysql.h>
#include <unistd.h>
#define SQLHANDLE static MYSQL
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
SQLHANDLE hDBEnv, hDBC;
int DB_Open(char * dbcn, char * usr, char * pwd)
{
int r;
#ifdef WIN32
r = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hDBEnv);
if(r) return 0;
r = SQLSetEnvAttr(hDBEnv,SQL_ATTR_ODBC_VERSION,
(void*)SQL_OV_ODBC3,0);
if(r) return 0;
r = SQLAllocHandle(SQL_HANDLE_DBC, hDBEnv, &hDBC);
if(r) return 0;
r = SQLConnect(hDBC,
(unsigned char *)dbcn, strlen(dbcn),
(unsigned char *)usr, strlen(usr),
(unsigned char *)pwd, strlen(pwd));
return r==SQL_SUCCESS || r==SQL_SUCCESS_WITH_INFO;
#else
mysql_init(&hDBC);
MYSQL * rx = mysql_real_connect(
&hDBC, dbcn, usr, pwd, NULL, 0, NULL, 0);
if(!rx) return 0;
return 1;
#endif
}//end DB_Open
int DB_Exec(char * sql)
{
#ifdef WIN32
SQLHANDLE hStatement = NULL;
SQLAllocHandle(SQL_HANDLE_STMT, hDBC, &hStatement);
SQLExecDirect(hStatement,(unsigned char *)sql, strlen(sql));
SQLCloseCursor(hStatement);
SQLFreeHandle(SQL_HANDLE_STMT, hStatement);
hStatement = NULL;
#else
mysql_real_query (&hDBC, sql, strlen(sql));
#endif
return 1;
}//end DB_Exec
int DB_Close(void)
{
#ifdef WIN32
SQLDisconnect(hDBC);
SQLFreeHandle(SQL_HANDLE_DBC, hDBC);
SQLFreeHandle(SQL_HANDLE_ENV, hDBEnv);
#else
mysql_close(&hDBC);
#endif
return 1;
}//DB_Close()
#ifndef WIN32
typedef struct tagMySQLRecordset {
MYSQL_RES * hRecord;
void * * row;
int * size;
int cols;
} MYSQLRecordset;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询