原文地址:
http://blog.sina.com.cn/s/blog_494e45fe0100k9p8.html 安裝MySQL
代碼:
sudo apt-get install mysql-server mysql-client 這個時候ubuntu不會像windows那樣會出現c api,要繼續安裝api
安
裝開發包
代碼:
sudo apt-get install libmysqlclient15-dev 代碼:
#include <mysql.h>
編譯方法:
代碼:
gcc $(mysql_config --cflags) xxx.c -o xxx $(mysql_config --libs)
#include <mysql.h>
#include <stdio.h>

main()
{
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "mysql";
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,

user, password, database, 0, NULL, 0))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

if (mysql_query(conn, "show tables"))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
mysql_free_result(res);
mysql_close(conn);
}

會輸出現有數據庫和表內容。 eclipse下要添加include路徑,和鏈接庫路徑,
還需要添加對 -lmysqlclient -lm兩個參數
項目->屬性->C/C++Build -> settings -> gcc c linker-> libraries
libraries(l) 中添加兩個參數mysqlclient和m
不然還是出項找不到mysql_init()的錯誤。
posted on 2011-05-03 13:34
漂漂 閱讀(2504)
評論(0) 編輯 收藏 引用 所屬分類:
linux