#include <mysql.h>
#include <stdio.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = ""; // set me first
char *database = "VirtualSoc";
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);
}这是全部代码。我试图连接到Linux下的数据库。
我如何编译这个呢?
为什么我说mysql.h doesn't exist时出错了
我需要使用像libmysqlclient或libmariadbclient使用mysql.h之类的东西吗?
我不想使用这些,我只是想连接到我的XAMPP服务器作为本地主机在我的机器上。我怎么能这么做?还有另一个问题:为什么我要像这样编译我的代码?
$gcc -o output-file $(mysql_config --c威) mysql-c-api.c $(mysql_config -libs)
发布于 2014-12-04 16:36:13
No,我找到了答案,对于那些想使用c来与xampp的服务器(Mysql)通话的人,您需要包括 #include "/opt/lampp/include/mysql.h" (这是报头),并且在/include文件夹中有所有定义为整天连接和执行查询的函数E 210、E 111宏E 212。谢谢你的回答,但你并没有支持我,你给了我一个非常错误的答案,,,我希望我能提前帮助那些想做同样事情的人。
我了解到,无论您想要将哪个客户端的服务组合在一起,绝大多数服务都是用orcle技术拥有和实现的,您需要安装一个sudo apt-get install libmysqlclient-dev,然后用
gcc -o example example.c `mysql_config --cflags --libs`发布于 2014-12-04 13:32:35
您的编译器无法在系统定义的mysql.h路径中找到include。要纠正这一点,您必须使用-I<path_to_header>和gcc。
在您的示例中,建议使用" "而不是<>和#include语句。
https://stackoverflow.com/questions/27295261
复制相似问题