看到 這一幕··我終於感動了·····c 連接mysql 成功

爲了寫oj ,必須要連上數據庫,又因爲評判我是在linux系統上開發的 ,

數據庫又在 windows 上··所以開始了 linux 連 windows遠程數據庫 的 艱難歷程·

 

首先,我得知道 數據庫怎麼連  google 之  幸有所得:

 

 #include <stdio.h>
 #include <mysql.h>
 

 int main() {
  MYSQL *conn;
  MYSQL_RES *res;
  MYSQL_ROW row;


  char *server = "localhost";
  char *user = "root";
  char *password = ""; /* 此處改成你的密碼 */
  char *database = "mysql";

 
  conn = mysql_init(NULL);


  /* Connect to database */
  if (!mysql_real_connect(conn, server,
  user, password, database, 0, NULL, 0)) {
  fprintf(stderr, "%s/n", mysql_error(conn));
  exit(1);
  }


  /* send SQL query */
  if (mysql_query(conn, "show tables")) {
  fprintf(stderr, "%s/n", mysql_error(conn));
  exit(1);
  }


  res = mysql_use_result(conn);


  /* output table name */

      printf("MySQL Tables in mysql database:/n");
  while ((row = mysql_fetch_row(res)) != NULL)
  printf("%s /n", row[0]);


  /* close connection */
  mysql_free_result(res);
  mysql_close(conn);

      return 0;
}

 

 

然後編譯···oh ! myGOD   很多錯,

具體一看  找不到頭文件 ·GOOGLE ····

原來需要 一個 rpm 包 MySQL-devel-standard-5.0.9-0.rhel4.i386.rpm

索性 把 mysql  也裝上  共 3個包

MySQL-server-standard-5.0.21-1.rhel4.i386.rpm (Server 數據庫) 
MySQL-client-standard-5.0.21-1.rhel4.i386.rpm (Client 客戶端)

MySQL-devel-standard-5.0.21-1.rhel4.i386.rpm (Headers and libraries 開發包)

下之···安裝

 

再編譯 不行··找不到庫 GOOGLE····

 

結果··必須 如下 OK

#gcc -g dbc.c -o dbc $(mysql_config --libs) $(mysql_config --cflags)

 

編譯 終於 好了

 

再是連接數據庫···連接失敗···再GOOGLE

 

原來有可能 防火牆問題 ,而且需要server 給 遠程客戶端 賦權

 

於是 grant all privileges on judgeonline.* to [email protected].*.* IDENTIFIED by 'root';

 

等待 幾秒後 ,終於  成功···

 

(本想 發圖的 ,發現 不能 發圖 失望啊····)

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章