14 个解决方案
#2
http://www.toplee.com/blog/329.html
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h" //我的机器上该文件在/usr/local/include/mysql下
//定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "select username from tbb_user where userid = %d"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*sock; //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *res; //查询结果集,结构类型
MYSQL_FIELD *fd ; //包含字段信息的结构
MYSQL_ROW row ; //存放一行查询结果的字符串数组
char qbuf[160]; //存放查询sql语句字符串
if (argc != 2) { //检查输入参数
fprintf(stderr,"usage : mysql_select <userid>\n\n");
exit(1);
}
mysql_init(&mysql);
if (!(sock = mysql_real_connect(&mysql,"localhost","dbuser","dbpwd","9tmd_bbs_utf8",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
perror("");
exit(1);
}
sprintf(qbuf,SELECT_QUERY,atoi(argv[1]));
if(mysql_query(sock,qbuf)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
exit(1);
}
if (!(res=mysql_store_result(sock))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(sock));
exit(1);
}
printf("number of fields returned: %d\n",mysql_num_fields(res));
while (row = mysql_fetch_row(res)) {
printf("Ther userid #%d 's username is: %s\n", atoi(argv[1]),(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0])) ;
puts( "query ok !\n" ) ;
}
mysql_free_result(res);
mysql_close(sock);
exit(0);
return 0; //. 为了兼容大部分的编译器加入此行
}
#3
用mysql++吧
.
.
#4
额,网上不是很多吗?baidu下吧哥们
#5
我写过这样一段代码,写得很烂,实在不好意思拿出来。你要的话,给我发邮件。其实很简单的。我的邮箱地址:zxy__11@163.com
#6
好久没接分了,接个
#7
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into T_Person(FAge,FName) values(20,'kider')",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
参考:http://www.rupeng.com/forum/thread-1403-1-2.html
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into T_Person(FAge,FName) values(20,'kider')",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
参考:http://www.rupeng.com/forum/thread-1403-1-2.html
#8
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into T_Person(FAge,FName) values(20,'kider')",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
#9
找关键字.在搜索引擎上捞一把不就行了。
#10
csdn回复框上面相关问题都有提示了..
相关问题
我在windows2000用c语言连接mysql数据库,编译出现错误,请帮忙看看 ... linux下C语言连接mysql数据库编程,如何捕获错误? Linux/Unix社区 ... c在linux下访问mysql数据库? C/C++ / C语言- CSDN社区community.csdn.net 【连接mysql问题】请问VB如何连接远程mysql数据库(服务器是Linux ... linux下连接mysql数据库问题Linux/Unix社区/ 程序开发区- CSDN社区 ... 200分~ 求python连接MySQL数据库的详细教程~ 急~ 其他开发语言/ 脚本 ...
相关问题
我在windows2000用c语言连接mysql数据库,编译出现错误,请帮忙看看 ... linux下C语言连接mysql数据库编程,如何捕获错误? Linux/Unix社区 ... c在linux下访问mysql数据库? C/C++ / C语言- CSDN社区community.csdn.net 【连接mysql问题】请问VB如何连接远程mysql数据库(服务器是Linux ... linux下连接mysql数据库问题Linux/Unix社区/ 程序开发区- CSDN社区 ... 200分~ 求python连接MySQL数据库的详细教程~ 急~ 其他开发语言/ 脚本 ...
#11
能给我一下你的那个#include "mysql.h" 吗?谢谢了!
#12
学习了。
#13
参看《LINUX程序设计第三版》
#14
看到有朋友不知道mysql.h在哪里,我来回答一下:/usr/include/mysql/目录下存在mysql.h,当然这是我的unbuntu9.10 mysql5.1默认安装路径。
#1
#2
http://www.toplee.com/blog/329.html
#if defined(_WIN32) || defined(_WIN64) //为了支持windows平台上的编译
#include <windows.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "mysql.h" //我的机器上该文件在/usr/local/include/mysql下
//定义数据库操作的宏,也可以不定义留着后面直接写进代码
#define SELECT_QUERY "select username from tbb_user where userid = %d"
int main(int argc, char **argv) //char **argv 相当于 char *argv[]
{
MYSQL mysql,*sock; //定义数据库连接的句柄,它被用于几乎所有的MySQL函数
MYSQL_RES *res; //查询结果集,结构类型
MYSQL_FIELD *fd ; //包含字段信息的结构
MYSQL_ROW row ; //存放一行查询结果的字符串数组
char qbuf[160]; //存放查询sql语句字符串
if (argc != 2) { //检查输入参数
fprintf(stderr,"usage : mysql_select <userid>\n\n");
exit(1);
}
mysql_init(&mysql);
if (!(sock = mysql_real_connect(&mysql,"localhost","dbuser","dbpwd","9tmd_bbs_utf8",0,NULL,0))) {
fprintf(stderr,"Couldn't connect to engine!\n%s\n\n",mysql_error(&mysql));
perror("");
exit(1);
}
sprintf(qbuf,SELECT_QUERY,atoi(argv[1]));
if(mysql_query(sock,qbuf)) {
fprintf(stderr,"Query failed (%s)\n",mysql_error(sock));
exit(1);
}
if (!(res=mysql_store_result(sock))) {
fprintf(stderr,"Couldn't get result from %s\n", mysql_error(sock));
exit(1);
}
printf("number of fields returned: %d\n",mysql_num_fields(res));
while (row = mysql_fetch_row(res)) {
printf("Ther userid #%d 's username is: %s\n", atoi(argv[1]),(((row[0]==NULL)&&(!strlen(row[0]))) ? "NULL" : row[0])) ;
puts( "query ok !\n" ) ;
}
mysql_free_result(res);
mysql_close(sock);
exit(0);
return 0; //. 为了兼容大部分的编译器加入此行
}
#3
用mysql++吧
.
.
#4
额,网上不是很多吗?baidu下吧哥们
#5
我写过这样一段代码,写得很烂,实在不好意思拿出来。你要的话,给我发邮件。其实很简单的。我的邮箱地址:zxy__11@163.com
#6
好久没接分了,接个
#7
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into T_Person(FAge,FName) values(20,'kider')",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
参考:http://www.rupeng.com/forum/thread-1403-1-2.html
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into T_Person(FAge,FName) values(20,'kider')",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
参考:http://www.rupeng.com/forum/thread-1403-1-2.html
#8
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include "resource.h"
#include "MainDlg.h"
#define LOGIN_TIMEOUT 30
#define MAXBUFLEN 255
#define CHECKDBSTMTERROR(hwnd,result,hstmt) if(SQL_ERROR==result){ShowDBStmtError(hwnd,hstmt);return;}
void ShowDBError(HWND hwnd,SQLSMALLINT type,SQLHANDLE sqlHandle)
{
char pStatus[10], pMsg[101];
SQLSMALLINT SQLmsglen;
char error[200] = {0};
SQLINTEGER SQLerr;
long erg2 = SQLGetDiagRec(type, sqlHandle,1,
(SQLCHAR *)pStatus,&SQLerr,(SQLCHAR *)pMsg,100,&SQLmsglen);
wsprintf(error,"%s (%d)\n",pMsg,(int)SQLerr);
MessageBox(hwnd,error,TEXT("数据库执行错误"),MB_ICONERROR|MB_OK);
}
void ShowDBConnError(HWND hwnd,SQLHDBC hdbc)
{
ShowDBError(hwnd,SQL_HANDLE_DBC,hdbc);
}
void ShowDBStmtError(HWND hwnd,SQLHSTMT hstmt)
{
ShowDBError(hwnd,SQL_HANDLE_STMT,hstmt);
}
void DBTest(HWND hwnd)
{
SQLHENV henv = NULL;
SQLHDBC hdbc = NULL;
SQLHSTMT hstmt = NULL;
SQLRETURN result;
SQLCHAR ConnStrIn[MAXBUFLEN] = "DRIVER={MySQL ODBC 5.1 Driver};SERVER=127.0.0.1;UID=root;PWD=root;DATABASE=test;CharSet=gbk;";
SQLCHAR ConnStrOut[MAXBUFLEN];
//分配环境句柄
result = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
//设置管理环境属性
result = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
//分配连接句柄
result = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性
result = SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (void*)LOGIN_TIMEOUT, 0);
//连接数据库
result = SQLDriverConnect(hdbc,NULL,
ConnStrIn,SQL_NTS,
ConnStrOut,MAXBUFLEN,
(SQLSMALLINT *)0,SQL_DRIVER_NOPROMPT);
if(SQL_ERROR==result)
{
ShowDBConnError(hwnd,hdbc);
return;
}
//初始化语句句柄
result = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
//SQL_NTS telling the function the previous parameter is Null-Terminated String,
//please alculate the string length for me
result = SQLPrepare(hstmt,(SQLCHAR*)"insert into T_Person(FAge,FName) values(20,'kider')",SQL_NTS);
CHECKDBSTMTERROR(hwnd,result,hstmt);
result =SQLExecute(hstmt);
CHECKDBSTMTERROR(hwnd,result,hstmt);
SQLFreeStmt(hstmt,SQL_CLOSE);
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC,hdbc);
SQLFreeHandle(SQL_HANDLE_ENV,henv);
MessageBox(hwnd,TEXT("执行成功"),TEXT("标题"),MB_OK);
}
#9
找关键字.在搜索引擎上捞一把不就行了。
#10
csdn回复框上面相关问题都有提示了..
相关问题
我在windows2000用c语言连接mysql数据库,编译出现错误,请帮忙看看 ... linux下C语言连接mysql数据库编程,如何捕获错误? Linux/Unix社区 ... c在linux下访问mysql数据库? C/C++ / C语言- CSDN社区community.csdn.net 【连接mysql问题】请问VB如何连接远程mysql数据库(服务器是Linux ... linux下连接mysql数据库问题Linux/Unix社区/ 程序开发区- CSDN社区 ... 200分~ 求python连接MySQL数据库的详细教程~ 急~ 其他开发语言/ 脚本 ...
相关问题
我在windows2000用c语言连接mysql数据库,编译出现错误,请帮忙看看 ... linux下C语言连接mysql数据库编程,如何捕获错误? Linux/Unix社区 ... c在linux下访问mysql数据库? C/C++ / C语言- CSDN社区community.csdn.net 【连接mysql问题】请问VB如何连接远程mysql数据库(服务器是Linux ... linux下连接mysql数据库问题Linux/Unix社区/ 程序开发区- CSDN社区 ... 200分~ 求python连接MySQL数据库的详细教程~ 急~ 其他开发语言/ 脚本 ...
#11
能给我一下你的那个#include "mysql.h" 吗?谢谢了!
#12
学习了。
#13
参看《LINUX程序设计第三版》
#14
看到有朋友不知道mysql.h在哪里,我来回答一下:/usr/include/mysql/目录下存在mysql.h,当然这是我的unbuntu9.10 mysql5.1默认安装路径。