2016-09-08 42 views
3

我寫了一個簡短的程序來嘗試連接到MySQL數據庫。使用C在Eclipse中退出值 - 它們是什麼意思?

#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) 

我不認爲這是很好的,但我也有不知道這意味着什麼。任何人都可以幫我解讀這個嗎?

+1

如果您在命令行界面中運行可執行文件,該怎麼辦? –

+1

請參閱[此評論](http://stackoverflow.com/questions/30550496/eclipse-c-c-terminated-exit-value-1073741515#comment49181071_30550496) –

+0

「hello world」程序是否會在此設置上運行? –

回答

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)可能更容易。

+1

感謝您運行此示例。我正在使用Windows。我無法得到任何東西來嘗試使用C與Eclipse連接MySQL。我最終轉換到Pelles C.起初我遇到類似的問題,但讀了一篇文章說,包括libmysql.dll在與可執行文件相同的文件夾。當我這樣做時,我終於成功了。當我用Eclipse做到這一點時,程序崩潰了。我沒有閱讀任何文檔,它告訴你如何處理動態的dll庫。至少我現在有一些工作,但我希望它是Eclipse。 – sax

+0

@sax如果你編程C,你有沒有試過Clion?我認爲Clion是一個好的IDE。 –