windows mingw mysql eclipsecdt
一,造libmysql.def文件
1, ${mysql_home}/include/libmySQL.def文件如果有直接用
2, 否则下载pexports工具,执行pexprots libmySQL.dll> libmySQL.def(libmySQL.dll在${mysql_home}/bin目录下一般会有)
二,转换成静态库
dlltool --input-def libmySQL.def --dllname libmySQL.dll --output-lib libmysql.a -k
三,配置Eclipse(静态库和动态库加法都是-L${DIR} -l${libname},但谁的优先级高呢?)
1, 加上库目录,如"${workspace_loc:/hello_mysql/libs}"
2, 加上libmysql.a的库名: mysql
3, 构建,报错: #ff0000 reference to `mysql_real_connect@32'之类的错
4, 修改def文件,将mysql_real_connect改成mysql_real_connect@32,其它错误同理,并重新生成静态库
5, 重新构建完成
四,示例程序(只须修改连接信息及表名)
#include
<
stdio.h
>
/* 下面这一行一定要加上 */
#include < my_global.h >
#include < mysql.h >
int main() {
MYSQL mysql;
MYSQL_RES * result;
MYSQL_ROW row;
int numrows, numcols, c;
mysql_init( & mysql);
if ( ! mysql_real_connect( & mysql, " localhost " , " opencart " , " opencart " , " opencart " , 0 , NULL, 0 )) {
fprintf(stderr, " Failed to connect to database: Error %d:%s\n " , mysql_errno( & mysql), mysql_error( & mysql));
}
char query[] = " SELECT * FROM product " ;
if (mysql_query( & mysql, query))
{
fprintf(stderr, " Error executing query: Error %d: %s\n " , mysql_errno( & mysql), mysql_error( & mysql));
}
result = mysql_use_result( & mysql);
if ( ! result) {
fprintf(stderr, " Error executing query: Error %d: %s\n " , mysql_errno( & mysql), mysql_error( & mysql));
}
numcols = mysql_num_fields(result);
while (row = mysql_fetch_row(result)) {
for (c = 0 ; c < numcols; c ++ ) {
printf( " %s\t " , row[c]);
}
printf( " \n " );
}
}
/* 下面这一行一定要加上 */
#include < my_global.h >
#include < mysql.h >
int main() {
MYSQL mysql;
MYSQL_RES * result;
MYSQL_ROW row;
int numrows, numcols, c;
mysql_init( & mysql);
if ( ! mysql_real_connect( & mysql, " localhost " , " opencart " , " opencart " , " opencart " , 0 , NULL, 0 )) {
fprintf(stderr, " Failed to connect to database: Error %d:%s\n " , mysql_errno( & mysql), mysql_error( & mysql));
}
char query[] = " SELECT * FROM product " ;
if (mysql_query( & mysql, query))
{
fprintf(stderr, " Error executing query: Error %d: %s\n " , mysql_errno( & mysql), mysql_error( & mysql));
}
result = mysql_use_result( & mysql);
if ( ! result) {
fprintf(stderr, " Error executing query: Error %d: %s\n " , mysql_errno( & mysql), mysql_error( & mysql));
}
numcols = mysql_num_fields(result);
while (row = mysql_fetch_row(result)) {
for (c = 0 ; c < numcols; c ++ ) {
printf( " %s\t " , row[c]);
}
printf( " \n " );
}
}