linux c編程訪問資料庫,linux訪問資料

來源:互聯網
上載者:User

linux c編程訪問資料庫,linux訪問資料

源碼如下:

#include <stdio.h>#include <stdlib.h>#include <mysql/mysql.h>int main(){    /*定義mysql變數*/    MYSQL *coon;        //資料庫連結時使用的控制代碼    MYSQL_RES *res;     //返回行的查詢結果    MYSQL_ROW row;      //記錄集中的一條資料    char server[] = "192.168.1.105";    char user[] = "gino";    char password[] = "123456";    char database[] = "mysql";    coon = mysql_init(NULL); //初始化,得到mysql控制代碼    /*串連資料庫*/    if( !mysql_real_connect(coon,server,user,password,database,0,NULL,0)){        fprintf(stderr,"%s\n",mysql_errno(coon));        exit(1);    }    /*執行mysql語句*/    if(mysql_query(coon,"show tables")){        fprintf(stderr,"%s\n",mysql_errno(coon));        exit(1);    }    /*初始化逐行的結果集*/    res = mysql_use_result(coon);    printf("MySQL Tables in mysql datables:\n");    //從結果集中取出下一行    while((row = mysql_fetch_row(res)) != NULL){        printf("%s\n",row[0]);    }    //釋放結果集使用多的記憶體    mysql_free_result(res);    //關閉伺服器串連    mysql_close(coon);    printf("finish\n");    return 0;}

使用mysql API後, 編譯需要加 -lmysqlclient 。

libmysqlclient 的安裝:

sudo apt-get install libmysqlclient-dev

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.