C++连接MYSQL

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) 嘿嘿郁闷了半天!

Feedback

# re: C++连接MYSQL  回复  更多评论   

2008-07-16 19:41 by banboo
我在vc里面打算链接mysql,但是编译时候出现以下错误,请问如何解决呢?谢谢
--------------------Configuration: mysql - Win32 Debug--------------------
Linking...
testmysql.obj : error LNK2001: unresolved external symbol _mysql_close@4
testmysql.obj : error LNK2001: unresolved external symbol _mysql_free_result@4
testmysql.obj : error LNK2001: unresolved external symbol _mysql_num_fields@4
testmysql.obj : error LNK2001: unresolved external symbol _mysql_fetch_row@4
testmysql.obj : error LNK2001: unresolved external symbol _mysql_store_result@4
testmysql.obj : error LNK2001: unresolved external symbol _mysql_real_query@12
testmysql.obj : error LNK2001: unresolved external symbol _mysql_error@4
testmysql.obj : error LNK2001: unresolved external symbol _mysql_real_connect@32
testmysql.obj : error LNK2001: unresolved external symbol _mysql_init@4
Debug/mysql.exe : fatal error LNK1120: 9 unresolved externals
Error executing link.exe.

mysql.exe - 10 error(s), 0 warning(s)

# re: C++连接MYSQL  回复  更多评论   

2008-09-15 16:35 by haiouzsf
加下面的语句:
#pragma comment(lib,"C:\\Program Files\\MySQL\\MySQL Server 5.0\\lib\\opt\\libmysql.lib")

# 关闭连接mysql时候报错!  回复  更多评论   

2008-09-17 23:04 by colorful
Error in my_thread_global_end(): 1 threads didn't exit
不知道为什么?

# re: C++连接MYSQL  回复  更多评论   

2015-06-30 16:41 by Elly
@banboo运行这段代码跟出现了同样的问题,我按照下面添加了相应的还是不行。请问你最终解决了吗
#pragma comment(lib,"C:\\Program Files\\MySQL\\MySQL Server 5.0\\lib\\opt\\libmysql.lib")

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


posts - 6, comments - 6, trackbacks - 0, articles - 0

Copyright © Lemo