我正在windows环境下使用CodeBlocks。以下是我用C语言连接MySQL数据库的代码:
/* Simple C program that connects to MySQL Database server*/
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
//set the password for mysql server here
char *password = "*********"; /* set me first */
char *database = "Real_flights";
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);
}
我遇到了一个错误弹窗,上面写着:
mysql.h : No such file or directory
在使用Codeblocks时,如果包括mysql.h文件并使用C++编写代码,则不会出现错误。但是,如果使用C语言编写代码,则会出现以上错误。
#include <mysql/mysql.h>
。 - alkmain()
函数的返回类型是int
,而不是空白。 - user3629249