Posted on 2008-01-10 12:46
Lemo 阅读(4959)
评论(4) 编辑 收藏 引用
#include <stdio.h>
#include <tchar.h>
#include <winsock.h>
#include <mysql.h>
#include <string.h>
#include <windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
MYSQL mysql; // need a instance to init
MYSQL_RES *res;
MYSQL_ROW row;
char *query;
int t,r;
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,"localhost", "root", "330210520", "text",0,NULL,0))
{
printf( "Error connecting to database: %s\n",mysql_error(&mysql));
}
else printf("Connected...\n");
query = "select * from t1";
t = mysql_real_query(&mysql,query,(unsigned int) strlen(query));
if (t)
{
printf("Error making query: %s\n",
mysql_error(&mysql));
}
else printf("[%s] made...\n", query);
res = mysql_store_result(&mysql);
while(row = mysql_fetch_row(res))
{
for(t=0;t<mysql_num_fields(res);t++)
{
printf("%s ",row[t]);
}
printf("\n");
}
printf("mysql_free_result...\n");
mysql_free_result(res);
// execute the insert query
query = "insert into t1(id, name) values(3, 'kunp')";
t = mysql_real_query(&mysql,query,(unsigned int) strlen(query));
if (t)
{
printf("Error making query: %s\n",
mysql_error(&mysql));
}
else printf("[%s] made...\n", query);
mysql_close(&mysql);
return 0;
}
刚开始做的时候出了很多问题!不过最重要是要包含socket文件!还有下面2个库 ! 配置了mysql数据库文件下的路径!一个是include还有一个是lib,
libmySQL.lib wsock32.lib 不明白的留言 我在写详细告诉你呵呵!vs下配置MySQL\MySQL Server 5.0\include 和C:\Program Files\MySQL\MySQL Server 5.0\lib\opt
刚试调用过程!发现原来要设置参数CLIENT_MULTI_STATEMENTS
mysql_real_connect(&mysql,"localhost", "root", "330210520", "text",0,NULL,0)改为mysql_real_connect(&mysql,"localhost", "root", "330210520", "text",0,NULL,CLIENT_MULTI_STATEMENTS) 嘿嘿郁闷了半天!