[做个参考]mysql链接c的代码,设置set names utf8。。。

jackxiang 2007-10-29 23:12 | |

#include <stdio.h>
#include <iostream>

#include <string.h>
#include <stdlib.h>
#include <math.h>
//#include "/usr/local/mysql/include/mysql/mysql.h"
#include "/usr/local/mysql/include/mysql.h"
using namespace std;
int main(void)
{
    cout << "Content-type: text/html \n\n";
    char mysqlServer[20] = "172.25.38.70";
    char query[300];
    MYSQL myData;
    MYSQL_RES *res;
    MYSQL_FIELD *fd;
    MYSQL_ROW row;
    int rowCount = 0;
    int colCount = 0;
    int i, j;

    mysql_init( &myData );
    if(!mysql_real_connect(&myData, mysqlServer, "root", "","test3",3306,NULL,0))  
    {
        printf("connect mysql error!\n");
        return 0;
    }
    mysql_query(&myData,"set names utf8");
    sprintf(query,"select * from test3");
    if( mysql_query(&myData, query) != 0 )
    {
        printf("query error!\n");
        return 0;
    }
    else
    {
        res = mysql_store_result( &myData );
        rowCount = (int) mysql_num_rows( res );
        colCount = (int) mysql_num_fields( res );

    printf("mysql result: %d records found\n fields: %d \n", rowCount, colCount);
    for(i = 0; i < rowCount; i++)
    {
      row = mysql_fetch_row( res );
      for( j = 0; j < colCount; j++)
      {
          printf("[ %s ] ", row[j] );
            printf(" \t");
      }
  printf("______ %d\n",i);
  printf(" \n ");
}
}

}

正确的编译命令代码:
g++ c_mysql.cpp -L/usr/local/mysql/lib/  -lmysqlclient -lz -lm -o cout

注意:
如果/tmp/ccTGmMS21.o: In function `main':
/tmp/ccTGmMS21.o(.text+0x11): undefined reference to `mysql_init'
那么参数增加-L/usr/lib/mysql -lmysqlclient  
如果
usr/lib/mysql/libmysqlclient.a(my_compress.o): In function `my_uncompress':
my_compress.o(.text+0xaa): undefined reference to `uncompress'
那么增加-lz参数
规范点: g++ c_mysql.cpp -L/usr/local/mysql/lib/  -lmysqlclient -lz -lm

作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:https://jackxiang.com/post/709/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: jackxiang 编辑于2010-10-29 16:40
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]