连接远程sql(c语言)

时间:2015-10-27 15:03:51

标签: mysql c

我无法连接远程mysql服务器。运行代码后使用gcc,有对mysql的未定义引用

administrator@el6116-1:~$ gcc pilihan.c -o pilihan /tmp/cc4UQV0F.o: In
    function `main': pilihan.c:(.text+0x67): undefined reference to
    mysql_init' pilihan.c:(.text+0xe1): undefined reference to
    mysql_real_connect' pilihan.c:(.text+0xf6): undefined reference to
    mysql_error' pilihan.c:(.text+0x135): undefined reference to
    mysql_query' pilihan.c:(.text+0x145): undefined reference to
    mysql_error' pilihan.c:(.text+0x177): undefined reference to
    mysql_use_result' pilihan.c:(.text+0x1c2): undefined reference to
    mysql_fetch_row' pilihan.c:(.text+0x1d9): undefined reference to
    mysql_free_result' pilihan.c:(.text+0x1e5): undefined reference to
    mysql_close' collect2: error: ld returned 1 exit status

这是我在c:

中的代码
#include <stdio.h>
#include <mysql/mysql.h>
#include <stdlib.h>

void DataA()
{
    printf( "DataNasabah Dipilih..." );
}
void DataB()
{
    printf( "AdminNasabah Dipilih..." );
}

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

 char *server = "167.205.65.251";
 char *user = "nasabah";
 char *password = "123"; 
 char *database = "wordpress";

 conn = mysql_init(NULL);

    int input;
    printf("MySQl Client Alfred Saut Sibarani 23214038\n");
    printf( "1. Data A\n" );
    printf( "2. Data B\n" );
    printf( "3. Keluar\n" );
    printf( "Pilih Database: " );
    scanf( "%d", &input );

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


      /*If input =2 send mysql query*/  
      if (input == 2 && mysql_query(conn, "SELECT * FROM DataNasabah")) 
    {
          fprintf(stderr, "%s\n",  mysql_error(conn));
          exit(1);      
        }

      res = mysql_use_result(conn);

      /* Show table output*/
      printf("Data Nasabah:\n");
      while ((row = mysql_fetch_row(res)) != NULL)
          printf("%s %s %s\n", row[0],row[1],row[2]);

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

} 

1 个答案:

答案 0 :(得分:0)

#include <mysql/mysql.h>

没有成功。您必须确保库已到位且正确链接。

undefined reference to
    mysql_init' pilihan.c:(.text+0xe1)

是一个明确的指示,表明您的库mysql.h链接失败。