2012-01-17 98 views
4

我已經安裝mySQL的連接MySQL - 使用使用利用</p> <p><code>sudo apt-get install libmysqlclient15-dev</code></p> <p>此外我已經安裝libmysqlC++</p> <p><code>sudo apt-get install mySQL-server</code></p> <p>然後我已經安裝libmysqlclient15-dev的通過C++

dev的

sudo apt-get install libmysqlc++-dev

這一切後我試着用

g++ test.c -I/usr/include/mysql -I/usr/include/mysql++

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


// just going to input the general details and not the port numbers 
struct connection_details 
{ 
    char *server; 
    char *user; 
    char *password; 
    char *database; 
}; 

MYSQL* mysql_connection_setup(struct connection_details mysql_details) 
{ 
    // first of all create a mysql instance and initialize the variables within 
    MYSQL *connection = mysql_init(NULL); 

    // connect to the database with the details attached. 
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) { 
     printf("Conection error : %s\n", mysql_error(connection)); 
     exit(1); 
    } 
    return connection; 
} 

MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query) 
{ 
    // send the query to the database 
    if (mysql_query(connection, sql_query)) 
    { 
     printf("MySQL query error : %s\n", mysql_error(connection)); 
     exit(1); 
    } 

    return mysql_use_result(connection); 
} 

int main() 
{ 
    MYSQL *conn;  // the connection 
    MYSQL_RES *res; // the results 
    MYSQL_ROW row; // the results row (line by line) 

    struct connection_details mysqlD; 
    mysqlD.server = "localhost"; // where the mysql database is 
    mysqlD.user = "root";  // the root user of mysql 
    mysqlD.password = "123"; // the password of the root user in mysql 
    mysqlD.database = "mysql"; // the databse to pick 

    // connect to the mysql database 
    conn = mysql_connection_setup(mysqlD); 

    // assign the results return to the MYSQL_RES pointer 
    res = mysql_perform_query(conn, "show tables"); 

    printf("MySQL Tables in mysql database:\n"); 
    while ((row = mysql_fetch_row(res)) !=NULL) 
     printf("%s\n", row[0]); 
    // clean up the database result set 
    mysql_free_result(res); 
    // clean up the database link 
    mysql_close(conn); 

    return 0; 
} 

運行下面的代碼,但我得到了以下錯誤::

[email protected]:~/mysqlC++$ g++ test.c -I/usr/include/mysql -I/usr/include/mysql++test.c: In function ‘int main()’: 
test.c:47:19: warning: deprecated conversion from string constant to ‘char*’ 
test.c:48:17: warning: deprecated conversion from string constant to ‘char*’ 
test.c:49:21: warning: deprecated conversion from string constant to ‘char*’ 
test.c:50:21: warning: deprecated conversion from string constant to ‘char*’ 
test.c:56:48: warning: deprecated conversion from string constant to ‘char*’ 
/tmp/ccHFL1M4.o: In function `mysql_connection_setup(connection_details)': 
test.c:(.text+0xf): undefined reference to `mysql_init' 
test.c:(.text+0x51): undefined reference to `mysql_real_connect' 
test.c:(.text+0x65): undefined reference to `mysql_error' 
/tmp/ccHFL1M4.o: In function `mysql_perform_query(st_mysql*, char*)': 
test.c:(.text+0xa2): undefined reference to `mysql_query' 
test.c:(.text+0xb6): undefined reference to `mysql_error' 
test.c:(.text+0xdd): undefined reference to `mysql_use_result' 
/tmp/ccHFL1M4.o: In function `main': 
test.c:(.text+0x170): undefined reference to `mysql_fetch_row' 
test.c:(.text+0x18c): undefined reference to `mysql_free_result' 
test.c:(.text+0x198): undefined reference to `mysql_close' 
collect2: ld returned 1 exit status 

我相信我所做的一切都是正確的..請大家指出我誤會的地方

回答

8

你有實際鏈接庫以及(-I只是規定了包括目錄)。

嘗試

g++ -I/usr/include/mysql -I/usr/include/mysql++ -L/usr/local/lib -lmysqlpp -lmysqlclient test.c 

Here's a sample makefile for you

這裏有一個單獨的編譯步驟的示例命令行(創建對象文件,然後再連接在一起):

g++ -I/usr/include/mysql -I/usr/include/mysql++ -o test.o -c test.c 
g++ -L/usr/local/lib -lmysqlpp -lmysqlclient -o test test.o 
+1

更好的是,分開編譯和鏈接階段,但這應該讓OP開始。 +1。 – 2012-01-17 15:12:30

+0

嘿謝謝 Chani 2012-01-17 15:36:32

2

使用這個命令編譯

gcc -o test -L/usr/lib/mysql -lmysqlclient test.c 

Source

+0

嘿謝謝 Chani 2012-01-17 15:36:46

2

要包括mysql++.h但沒有使用它。

我改變你的代碼有點

#include <mysql/mysql.h> 
#include <stdio.h> 
#include <stdlib.h> 
/* #include <mysql++.h> Do not need this */ 

並編譯它。沒有warnig。

gcc mysql-test.c $(mysql_config --cflags) $(mysql_config --libs) -Wall 

它運行。

$ ./a.out 
Conection error : Access denied for user 'root'@'localhost' (using password: YES) 

似乎你在錯誤陳述中有拼寫錯誤。這證明了你的代碼。

+0

嘿謝謝 Chani 2012-01-17 15:36:57

相關問題