2012-03-18 2 views
0

얘들 아, 문제는 내가 그것을 서버가 연결되지 않았거나 경우에 저에게 할 수있는 프로그램을 걸리는MySQL의 타임 아웃 - C/C++이 코드 문제에 직면하고있어

시간을 의미하는 제한 시간으로 제한 시간입니다. 난 내

로컬 호스트를 사용하는 경우 난 빠른 답변을 얻을,하지만 난 내 로컬 호스트 외부에 연결할 때이 50sc합니다 -이 완료 될 때까지 응답과 프로그램 frezz에

1.5 분. HOw 나는 frezzing을 고칠 수 있습니까? 아니면

내 자신의 타임 아웃은 50sc 이후에도 계속 기다리는 것과 같이 연결이 실패하고 멈춘다 고 말하면 어떨까요?

내가 더 나은 어떤 도움을 주셔서 감사합니다 그것을 이해할 렸기 때문에 도움으로 코드를 사용하시기 바랍니다 내가 얻을

PS : 당신의 주요 관심사 (이하 "정지"의 경우 MAC

#include "mysql.h" 
#include <stdio.h> 
#include <stdlib.h> 


// Other Linker Flags: -lmysqlclient -lm -lz 

// just going to input the general details and not the port numbers 
struct connection_details 
{ 
    char *server; 
    char *user; 
    char *password; 
    char *database; 
}; 

MYSQL* mysql_connection_setup(struct connection_details mysql_details) 
{ 
    // first of all create a mysql instance and initialize the variables within 
    MYSQL *connection = mysql_init(NULL); 

    // connect to the database with the details attached. 
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) { 
     printf("Conection error : %s\n", mysql_error(connection)); 
     exit(1); 
    } 
    return connection; 
} 



MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query) 
{ 
    // send the query to the database 
    if (mysql_query(connection, sql_query)) 
    { 
     printf("MySQL query error : %s\n", mysql_error(connection)); 
     exit(1); 
    } 

    return mysql_use_result(connection); 
} 

int main() 
{ 

     MYSQL *conn;  // the connection 
    MYSQL_RES *res; // the results 
    MYSQL_ROW row; // the results row (line by line) 

    struct connection_details mysqlD; 
    mysqlD.server = (char*)"Localhost"; // where the mysql database is 
    mysqlD.user = (char*)"root";  // the root user of mysql 
    mysqlD.password = (char*)"123456"; // the password of the root user in mysql 
    mysqlD.database = (char*)"test"; // the databse to pick 

    // connect to the mysql database 
    conn = mysql_connection_setup(mysqlD); 

    // assign the results return to the MYSQL_RES pointer 
    res = mysql_perform_query(conn, (char*) "SELECT * FROM me"); 


    printf("MySQL Tables in mysql database:\n"); 
    while ((row = mysql_fetch_row(res)) !=NULL) 
     printf("%s - %s\n", row[0], row[1], row[2]); // <-- Rows 

    /* clean up the database result set */ 
    mysql_free_result(res); 
    /* clean up the database link */ 
    mysql_close(conn); 

    return 0; 
} 
+0

타임 아웃은 TCP 스택 내에 있습니다. 조정할 때 뭔가를 할 수는 있지만 플랫폼에 알릴 때까지는 할 수 없습니다. – trojanfoe

+0

Mac을 플랫폼으로 사용하는 경우 – user1262876

+0

로컬 호스트 외부에서 코드를 구현할 때 코드의 기본 사항을 변경 하시겠습니까? 'mysqlD.server = (char *) "Localhost"; "는 비 로컬 호스트 서버에서 이해하지 못합니다. 또한 다른 호스트에 구현할 때 연결합니까? 언젠가는 얼어 버린다고했는데 연결 되나요? – TheNavigat

답변

0

사용하는 이 연결 시도가 이루어지는 동안 UI가 응답하지 않는다고 생각하는 경우) 백그라운드에서 연결 코드를 실행하는 스레드를 만들어야하고 "연결 중 ..."이라는 대화 상자를 표시해야합니다 (또는 무언가를 "취소"버튼과 함께 누름으로써 사용자는 포기할 수 있습니다. 이 사이트 (또는 다른 사이트)를 검색해보십시오.이를 수행하는 방법에 대한 많은 예제와 토론이 있습니다.

+0

나는 그것을하는 법을 보여주는 당신에게서 예를 좋아할 것입니다, 그것은 당신이 그것을 정말 좋게 만들 수 있다는 것을 알기 때문에, 너무 힘들어 야합니다. – user1262876

+1

죄송합니다. Mac에서 한 줄의 코드를 작성한 적이 없으며 사용중인 GUI 라이브러리가 무엇인지 전혀 알지 못합니다. Google을 사용해보십시오. 너의 친구 야! (StackOverflow도 나쁘지 않습니다!) – aldo