C语言通过mysql的 C API访问MySQL

五 23rd, 2009
#include <mysql.h>
#include <stdio.h>
int main(){
    char host[32] = "localhost";
    char user[32] = "root";
    char password[32] = "123123";
    char db[32] = "wordpress";
    char sql[100];
    MYSQL mysql;
    MYSQL_RES *res;
    MYSQL_ROW row;
    mysql_init(&mysql);
    mysql_real_connect(&mysql,host,user,password,db,3306,NULL,0);
    gets(sql);
    mysql_query(&mysql,sql);
    if(mysql_errno(&mysql) > 0){
        printf("%s\n",mysql_error(&mysql));
        return 1;
    }else{
        res = mysql_store_result(&mysql);
    }
    while((row = mysql_fetch_row(res))){
        printf("%s\t%s\n",row[0],row[1]);
    }
    mysql_close(&mysql);
    return 0;
}

编辑选项(还不是很清楚,待研究后补充)
-L指定库的路径,
-l指定具体的库文件。
gcc -g -o my my.c -L./ -I./ `mysql_config –cflags –libs`

MySQL C API参考

标签:
目前还没有任何评论.
;) :| :x :twisted: :roll: :oops: :o :mrgreen: :lol: :idea: :evil: :cry: :arrow: :P :D :?: :? :) :( :!: 8O 8)

Spam Protection by WP-SpamFree