很着急的问题!求救c++的mysql连接!非常感谢
fuyongjun
|
1#
fuyongjun 发表于 2002-08-05 12:15
很着急的问题!求救c++的mysql连接!非常感谢
下面是我用c++编写的连接mysql数据库的代码!#include <stdio.h>
#include <string.h> #include <mysql/mysql.h> #define TRUE 1 #define FALSE 0 //--------------------------------------------------------------------------- int main() { char szTargetDSN[] = "bbs"; char szSqlText[500]=""; char aszFlds[ 25 ][ 25 ]; MYSQL * myData ; MYSQL_RES * res ; MYSQL_FIELD * fd ; MYSQL_ROW row ; int i,j,k; bool bCreate = TRUE; if ( (myData = mysql_init((MYSQL*) 0)) && mysql_real_connect( myData, "localhost","root", "pwd", szTargetDSN, MYSQL_PORT, NULL, 0 ) ) //初始化数据结构 连接数据库 { if(bCreate) { sprintf(szSqlText,"create table mytable1 (time datetime, s1 char(6),s2 char(11), s3 int, s4 int)");//构造SQL语句//新建一张表 if (mysql_query( myData, szSqlText)) //执行SQL语句 {//执行SQL语句出错 printf( "Can't create table") ; mysql_close( myData ) ; return 0; } } sprintf(szSqlText,"insert into mytable1 values('2000-3-10 21:01:30','Test','MySQLTest',2000,3)"); //向表中插入数据 //注意时间的格式 if (mysql_query( myData, szSqlText)) {//执行SQL语句出错 printf( "Can't insert data to table") ; mysql_close( myData ) ; return 0; } sprintf(szSqlText, "select * from mytable1 "); if (mysql_query( myData, szSqlText)) //进行数据检索 { //执行SQL语句出错 mysql_close( myData ) ; return 0 ; } else { res = mysql_store_result( myData ) ; //取得查询结果 i = (int) mysql_num_rows( res ) ; //取得有效记录数 printf( "Query: %s\n%ld records found:\n", szSqlText, i ) ; for ( i = 0 ; fd = mysql_fetch_field( res ) ; i++ ){ strcpy( aszFlds[ i ], fd->name ) ;//取得各字段名 } for (i=1; row = mysql_fetch_row( res ); ){ j = mysql_num_fields( res ) ;//依次读取各条记录 printf( "Record #%ld:-\n", i++ ) ; //取得记录中的字段数 for ( k = 0 ; k < j ; k++ ) //输出各字段的值 printf( " Fld #%d (%s): %s\n", k + 1, aszFlds[ k ], (((row[k]==NULL)|| (!strlen(row[k])))?"NULL":row[k])) ; puts( "==============================\n" ) ; } mysql_free_result( res ) ; } } else {//连接数据库出错 printf( "Can't connect to the mysql server ") ; mysql_close( myData ) ; return 0; } mysql_close( myData ) ; return 0; } 关于mysql的函数库我已经拷贝到了/usr/lib/下了!!! #include <mysql/mysql.h>是绝对没有错误的! 同时我也把lib/mysql/下的文件都栲到响应的文件下了!!!! 可能给连接有关! 可在编译的时候还是出现了: [chat1@chat ~/exe]$ gcc fu.cxx fu.cxx:76:1: warning: no newline at end of file /tmp/ccXNCIlI.o: In function `main': /tmp/ccXNCIlI.o(.text+0x3a): undefined reference to `mysql_init' /tmp/ccXNCIlI.o(.text+0x79): undefined reference to `mysql_real_connect' /tmp/ccXNCIlI.o(.text+0xbb): undefined reference to `mysql_query' /tmp/ccXNCIlI.o(.text+0xe2): undefined reference to `mysql_close' /tmp/ccXNCIlI.o(.text+0x11c): undefined reference to `mysql_query' /tmp/ccXNCIlI.o(.text+0x143): undefined reference to `mysql_close' /tmp/ccXNCIlI.o(.text+0x17c): undefined reference to `mysql_query' /tmp/ccXNCIlI.o(.text+0x193): undefined reference to `mysql_close' /tmp/ccXNCIlI.o(.text+0x1ae): undefined reference to `mysql_store_result' /tmp/ccXNCIlI.o(.text+0x1c7): undefined reference to `mysql_num_rows' /tmp/ccXNCIlI.o(.text+0x20a): undefined reference to `mysql_fetch_field' /tmp/ccXNCIlI.o(.text+0x27a): undefined reference to `mysql_fetch_row' /tmp/ccXNCIlI.o(.text+0x29c): undefined reference to `mysql_free_result' /tmp/ccXNCIlI.o(.text+0x2b2): undefined reference to `mysql_num_fields' /tmp/ccXNCIlI.o(.text+0x3ce): undefined reference to `mysql_close' /tmp/ccXNCIlI.o(.text+0x3e6): undefined reference to `mysql_close' collect2: ld returned 1 exit status 请高手给予指点!要具体一点!谢谢 是不是跟libmysqlclient.so有关系呢?? |