在ubuntu系统安装了mysql后,通过调用mysql API,进行数据读写。
char *query_str;
int rc, i, fields;
int rows;
if (NULL == mysql_init(&stMysql))
{
fprintf(stderr, "mysql init error:%s\n", mysql_error(&stMysql));
return MY_SQL_ERROR;
}
if (mysql_real_connect(&stMysql, server, user, password, database, 0, NULL, 0) == NULL)
{
fprintf(stderr, "mysql_real_connect error:%s\n", mysql_error(&stMysql));
return MY_SQL_ERROR;
}
printf("Query:Connected Mysql server successfully\n");
query_str = "select * from devtable";
rc = mysql_real_query(&stMysql, query_str, strlen(query_str));
if (0 != rc)
{
printf("mysql_real_query():%s\n", mysql_error(&stMysql));
return MY_SQL_ERROR;
}
res = mysql_store_result(&stMysql);
if (NULL == res)
{
printf("mysql_restore_result():%s\n", mysql_error(&stMysql));
return MY_SQL_ERROR;
}
rows = mysql_num_rows(res);
printf("The total rows is: %d\n", rows);
fields = mysql_num_fields(res);
printf("The total fields is: %d\n", fields);
while ((row = mysql_fetch_row(res)))
{
for (i = 0; i < fields; i++)
{
printf("%s\t", row[i]);
}
printf("\n");
mysql_free_result(res);
mysql_close(&stMysql);
return MY_SQL_SUCCESS;
编译提示
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `add_plugin':
(.text+0x253): undefined reference to `dlclose'
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_client_plugin_deinit':
(.text+0x2c7): undefined reference to `dlclose'
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x524): undefined reference to `dlopen'
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x54a): undefined reference to `dlsym'
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x67f): undefined reference to `dlerror'
/usr/lib/i386-linux-gnu/libmysqlclient.a(client_plugin.c.o): In function `mysql_load_plugin_v':
(.text+0x695): undefined reference to `dlclose'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x52): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x60): undefined reference to `log'
/usr/lib/i386-linux-gnu/libmysqlclient.a(dh.cpp.o): In function `TaoCrypt::DH::GeneratePrivate(TaoCrypt::RandomNumberGenerator&, unsigned char*)':
(.text+0x72): undefined reference to `pow'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_getsystime.c.o): In function `my_getsystime':
(.text+0x29): undefined reference to `clock_gettime'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_compress_alloc':
(.text+0x68): undefined reference to `compress'
/usr/lib/i386-linux-gnu/libmysqlclient.a(my_compress.c.o): In function `my_uncompress':
解决这个问题,需要链接-ldl, dl库是用来做动态库加载的(dynamic load),将-ldl链接选项改到最后才可以
../mysql/libdb_mysql.a(db_mysql.o): In function `MY_mysql_query':
db_mysql.c:(.text+0x7c): undefined reference to `mysql_init'
db_mysql.c:(.text+0x8c): undefined reference to `mysql_error'
db_mysql.c:(.text+0x107): undefined reference to `mysql_real_connect'
db_mysql.c:(.text+0x117): undefined reference to `mysql_error'
db_mysql.c:(.text+0x16a): undefined reference to `mysql_real_query'
db_mysql.c:(.text+0x17a): undefined reference to `mysql_error'
db_mysql.c:(.text+0x1a8): undefined reference to `mysql_store_result'
db_mysql.c:(.text+0x1ba): undefined reference to `mysql_error'
db_mysql.c:(.text+0x1e4): undefined reference to `mysql_num_rows'
db_mysql.c:(.text+0x204): undefined reference to `mysql_num_fields'
db_mysql.c:(.text+0x256): undefined reference to `mysql_fetch_row'
db_mysql.c:(.text+0x26f): undefined reference to `mysql_free_result'
db_mysql.c:(.text+0x27b): undefined reference to `mysql_close'
../mysql/libdb_mysql.a(db_mysql.o): In function `MY_mysql_insert':
db_mysql.c:(.text+0x328): undefined reference to `mysql_init'
db_mysql.c:(.text+0x33b): undefined reference to `mysql_error'
db_mysql.c:(.text+0x3b9): undefined reference to `mysql_real_connect'
db_mysql.c:(.text+0x3cc): undefined reference to `mysql_error'
db_mysql.c:(.text+0x482): undefined reference to `mysql_real_query'
db_mysql.c:(.text+0x495): undefined reference to `mysql_error'
db_mysql.c:(.text+0x4c3): undefined reference to `mysql_close'
collect2: ld returned 1 exit status
使用mysql client API, 需要使用libmysqlclient.a, 需要查看安装Mysql后,这个库在哪个目录
我的服务器是在目录/usr/lib/i386-linux-gnu/
所以编译时候指明-L/usr/lib/i386-linux-gnu/ 路径,
同时通过mysql_config命令查看LIB依赖
usage: /usr/bin/mysql_config [OPTIONS]
Options:
--cflags [-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g]
--include [-I/usr/include/mysql]
--libs [-L/usr/lib/i386-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -ldl]
--libs_r [-L/usr/lib/i386-linux-gnu -lmysqlclient_r -lpthread -lz -lm -lrt -ldl]
--plugindir [/usr/lib/mysql/plugin]
--socket [/var/run/mysqld/mysqld.sock]
--port [0]
--version [5.5.54]
--libmysqld-libs [-L/usr/lib/i386-linux-gnu -lmysqld -lpthread -lz -lm -lrt -lwrap -lcrypt -ldl]
--variable=VAR VAR is one of:
pkgincludedir [/usr/include/mysql]
pkglibdir [/usr/lib/i386-linux-gnu]
plugindir [/usr/lib/mysql/plugin]
test@ubuntu:~/air/mysql$ mysql_config --libs
-L/usr/lib/i386-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -ldl
发现还需要-lmysqlclient -lpthread -lz -lm -lrt -ldl这些库,
链接加上这些库后,编译通过。
阅读(9460) | 评论(0) | 转发(0) |