程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> C語言:獲取某個mysql數據庫中所有的表及其表中所有的字段名

C語言:獲取某個mysql數據庫中所有的表及其表中所有的字段名

編輯:關於C
程序簡介:有時候我們想知道一個數據庫中到底有哪些表,表中都有些什麼字段。我寫了一個小程序來實現這個功能。     思路: 1:連接數據庫(廢話) 2:獲取數據庫中所有的表,並將它們緩存下來。   3:對於每個表,就執行SQL命令select * from XXX,並將它們的表頭輸出(如果大家能想到更好的方法,望告知)。   上代碼:     [cpp]   #include <stdio.h>     #include <stdlib.h>     #include <string.h>   #include <mysql/mysql.h>        #define MAX_COUNT 256      //發生錯誤時,輸出錯誤信息,關閉連接,退出程序     void error_quit(const char *str, MYSQL *connection)     {         fprintf(stderr, "%s : %d: %s\n",             str, mysql_errno(connection),             mysql_error(connection));         if( connection != NULL )             mysql_close(connection);         exit(1);     }        int main(int argc, char *argv[])      {         MYSQL *my_con;       MYSQL_RES *my_res;         MYSQL_FIELD *my_field;         MYSQL_ROW my_row;         int rows, i, j, res;         char namebuf[MAX_COUNT][MAX_COUNT] = { 0 };       int count = 0;          my_con = malloc( sizeof(MYSQL) );            //連接數據庫         mysql_init(my_con);          my_con = mysql_real_connect(my_con, "127.0.0.1", "root", "aaaaaaa",             "test", 0, NULL, CLIENT_FOUND_ROWS);         if( NULL == my_con )              error_quit("Connection fail", my_con);         printf("Connection success\n");            //獲取整個數據庫中的所有表       res = mysql_query(my_con, "show tables;");       if( res != 0 )             error_quit("Select fail", my_con);         my_res = mysql_store_result(my_con);         if( NULL == my_res )             error_quit("Get result fail", my_con);                //緩沖剛才查詢的結果       while( 1 )         {             my_row = mysql_fetch_row(my_res);             if( NULL == my_row )                 break;             if( my_row[0] == NULL )                 printf("NULL\t");             else                 strcpy(namebuf[count++], (char*)my_row[0]);       }            for(i=0; i<count; i++)       {           char tbuf[100] = { 0 };           snprintf(tbuf, 100, "select * from %s", namebuf[i]);           //獲取整個表的內容的指針           res = mysql_query(my_con, tbuf);             if( res != 0 )                 error_quit("Select fail", my_con);             my_res = mysql_store_result(my_con);             if( NULL == my_res )                 error_quit("Get result fail", my_con);                //獲取表的列數             rows = mysql_num_fields(my_res);            printf("name: %s   count: %d\n", namebuf[i], rows);              //獲取並輸出表頭             my_field = mysql_fetch_fields(my_res);             for(j=0; j<rows; j++)                 printf("%s, ", my_field[j].name);             printf("\n-------------------------------------\n\n");         }          //釋放空間,關閉連接         mysql_free_result(my_res);         mysql_close(my_con);         free(my_con);            return 0;     }       運行示例(紅色字體的是輸入): qch@LinuxMint ~/program/tcode $ gcc ctemp.c -o ctemp -lmysqlclient qch@LinuxMint ~/program/tcode $ ./ctemp Connection success name: class1   count: 4 id, name, age, birthday,  -------------------------------------     
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved