如何使用C\C ++连接mysql服务器? [英] How to connect mysql server using C\C++?
本文介绍了如何使用C\C ++连接mysql服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只想在windows xp上使用c连接mysql服务器,并在数据库中添加一些字段。我尝试了以下代码,但发现错误,如无法包含mysql.h。谁能帮我?在此先感谢。
I just want to connect mysql server using c on windows xp and add some fields to the data base. I tried the following code but geting an error like unable to include mysql.h. Can any one help me? Thanks in advance.
#include<mysql.h>
#include<stdio.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "user";
char *database = "";
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));
return(0);
}
/* send SQL query */
if (mysql_query(conn, "SELECT * FROM people WHERE age > 30")) {
fprintf(stderr, "%s\n", mysql_error(conn));
return(0);
}
res = mysql_use_result(conn);
/* output fields 1 and 2 of each row */
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s %s\n", row[1], row[2]);
/* Release memory used to store results and close connection */
mysql_free_result(res);
mysql_close(conn);
}
推荐答案
您需要将包含和库添加到编译中。您可以在库中找到并添加mysqlclient.lib,好像它没有添加到您的IDE /编译器中。
You need to add your includes and libs to the compilation. You can find and add mysqlclient.lib to your libraries, seems it is not added to your IDE/compiler.
我强烈建议您使用 MySQL ++ [ ^ ]。直接使用MySQL C API要容易得多。
-Saurabh
I will highly recommend using MySQL++[^]. It is much easier then using MySQL C API directly.
-Saurabh
只需转到项目属性 - >配置属性 - >链接器。
转到 - >常规并设置'附加库目录'指向你的lib文件目录。
然后转到链接器 - >输入并输入您的lib文件名,例如'附加依赖关系'字段中的mysqlclient.lib。
这是针对VC ++的,如果你使用其他编译器,那么你需要搞清楚。我希望这对你有用。
你甚至可以尝试这个链接在Visual Studio 2005 Express中使用库 [ ^ ]
Just go to project properties -> Configuration properties -> Linker.
Goto to ->General and set the 'Additional Library directories' to point to your lib file directory.
Then goto to Linker -> Input and type in your lib file name e.g. mysqlclient.lib in the 'Additional Dependencies' field.
This is for VC++, if you are using some other compiler then you need to figure it out. I hope this should work for you.
You can even try this link Using libraries with Visual Studio 2005 Express[^]
这篇关于如何使用C\C ++连接mysql服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文