我编写了一个简短的程序来尝试连接MySQL数据库。
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main () {
MYSQL *conn;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "database";
int port = 3306;
conn = mysql_init(NULL);
mysql_real_connect(conn, server,user,password,database, port, NULL, 0);
return 0;
}
它构建正常,但是当我运行它时,控制台会读取
<terminated> (exit value: -1,073,741,515)
我认为这不好,但我也不知道这意味着什么。任何人都可以帮我解读一下吗?
答案 0 :(得分:0)
这个程序适用于我使用Ubuntu Linux进行全新的mysql 5.7安装。
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "<passwrd>"; /* set me first */
char *database = "mysql";
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);
}
测试
./a.out
MySQL Tables in mysql database:
columns_priv
db
engine_cost
event
func
general_log
gtid_executed
help_category
help_keyword
help_relation
help_topic
innodb_index_stats
innodb_table_stats
ndb_binlog_index
plugin
proc
procs_priv
proxies_priv
server_cost
servers
slave_master_info
slave_relay_log_info
slave_worker_info
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user
请检查您的mysql是否正在运行并接受连接,并且程序正确终止。使用命令行链接mysql.h
可能更容易(我在编译上面时用gcc main.c -lmysqlclient
做了)。