我在Windows环境中使用代码块。这是我连接到mysql数据库的c代码。
/* 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
什么时候在代码块中使用c ++,包括mysql.h没有问题。但是当我使用c执行它时,会弹出上面的错误。
要在Windows机器上使用mysql,必须下载mysql connector for windows。在安装了一个离开默认值之后,可以在下面找到头文件和lib文件。
C:\ Program Files \ MySQL \ Connector C ++ 8.0
C:\ Program Files \ MySQL \ MySQL Server 8.0
通常,您必须为您检测正确的标题和库并正确链接它们。请参考this tutorial