樹心幽徑

« 20180608安裝並使用osCommerce | Main | 20180610在ubuntu用c在mysql ilvssell資料庫建立資料表new_id_reg »

20180609在ubuntu用c列出mysql ilvssell資料庫三個資料表的內容
2018/06/09,10:43

(1)列出mysql ilvssell資料庫三個資料表的內容 t9.c 程式碼摘述

:

void finish_with_error(MYSQL *con)
{
  fprintf(stderr, "%s\n", mysql_error(con));
  mysql_close(con);
  exit(1);       
}
const char * tab[] = {
    "new_cust",
    "new_goods",
    "new_factory"
};
#define n_tab (sizeof (tab) / sizeof (const char *))

:

  mysql_query(con, "set names utf8");
for (int i = 0; i < n_tab; i++) {
  printf ("\n%d: %s\n", i, tab[i]);

  char st[80];
//sprintf(st,"SELECT column_name from information_schema.columns where table_name = \"%s\" ",tab[i]);
  sprintf(st,"select * from %s",tab[i]);
  if (mysql_query(con, st)) finish_with_error(con);
  printf("SQL: [ %s ] Execute ok! \n\n", st);
  MYSQL_RES *result = mysql_store_result(con);
  if (result == NULL)
  {
      finish_with_error(con);
  }


  int num_fields = mysql_num_fields(result);

  MYSQL_FIELD *field;
  char *headers[num_fields];
  for(int k = 0; (field = mysql_fetch_field(result)); k++) {
    headers[k] = field->name;
    printf("%s, ",headers[k]);
   }
  printf("\n");

  MYSQL_ROW row;
  while ((row = mysql_fetch_row(result)))
  {
      for(int i = 0; i < num_fields; i++)
      {
          printf("%s, ", row[i] ? row[i] : "NULL");
      }
          printf("\n");
  }
  mysql_free_result(result);
 }

 

:

 

(2)編譯與執行:

treehrt@treehrt-fire:~/mysqlc$ gcc t9.c `mysql_config --cflags --libs`

treehrt@treehrt-fire:~/mysqlc$ ./a.out

0: new_cust
SQL: [ select * from new_cust ] Execute ok!

uid, remain, inyear, dept, grade, class, cardid, uname, pid, addr, tel, oldclass, sno, lostc, gen_code, in_use, cid4,
x, 1000, 107, x, 1, x, 512001, 王一志, x, x, x, x, x, 0, 0, 1, B301,

1: new_goods
SQL: [ select * from new_goods ] Execute ok!

gdsno, gdsname, spec, cost, price_in, price_special, price_sell, unit, stock_qty, fac_no, mem,
a001, 蕃茄, 個, 10, 10, 20, 20, 個, 200, 077, 金農行,

2: new_factory
SQL: [ select * from new_factory ] Execute ok!

facno, facname, simplify, phone1, phone2, fax, intername, interphone, master, facaddr, comaddr, lastdeal, rate, invoice, accountd, ps,
707, 金門農場, -, 082-333274, -, -, -, 082-333274, 吳小花, -, addr, 0, 0, 0, 123456, -,
treehrt@treehrt-fire:~/mysqlc$


 

迴響

 
Accessible and Valid XHTML 1.0 Strict and CSS Powered by LifeType