2011-03-08 2 views
0

mysoh 호출을 만들기 위해 autohotkey를 사용하고 있습니다. mysql 인터페이스는 mysql에 대한 시각적 기본 API를 참조하여 해독되었다. 내가 MySQL을 사용하고mysql이 libmysql.dll을 호출하여 mysql 시간 초과 후 자동으로 다시 연결되도록합니다.

이 게시물에 언급 된 통화를 연결합니다 http://www.autohotkey.com/forum/viewtopic.php?t=12482

mysql_options(mysql, MYSQL_OPT_RECONNECT, &true); 

그것은 나의 이해

내가 mysql_options이 펄 전화를 복제 할 dllcall을 추가하고 싶습니다

...되는 이 호출을 사용하면 표준 8 시간 mysql 시간 초과 후 내 프로그램을 정상적으로 mysql에 다시 연결할 수 있습니다. 내 신청서가 무한정 남아 있어야합니다.

여기 내 코드입니다. googles 소스 코드 라이브러리에 대한 참조는 재 연결 상수가 20임을 암시합니다. 모든 것은 mysql_opt_reconnect 호출을 제외하고 작동합니다.

libmysql.dll에 대한 적절한 호출을 결정하여 내 응용 프로그램이 mysql 시간 초과가 발생한 후 자동으로 다시 연결되도록 할 수 있습니까?

;============================================================ 
; mysql.ahk 
; 
; Provides a set of functions to connect and query a mysql database 
;============================================================ 

FileInstall, libmysql.dll, %A_AppData%\libmysql.dll, 1 

;============================================================ 
; Connect to mysql database and return db handle 
; 
; host = DTWRO-WS0061 
; user = alan 
; password = ******* 
; database = rush 
;============================================================ 

dbConnect(host,user,password,database){ 

    if (A_IsCompiled) { 
     ExternDir := A_AppData 
    } else { 
     ExternDir := A_WorkingDir 
    } 

    hModule := DllCall("LoadLibrary", "Str", ExternDir "\libmySQL.dll") 

    If (hModule = 0) 
    { 
     MsgBox 16, MySQL Error 233, Can't load libmySQL.dll from directory %ExternDir% 
     ExitApp 
    } 

    db := DllCall("libmySQL.dll\mysql_init", "UInt", 0) 

    If (db = 0) 
    { 
     MsgBox 16, MySQL Error 445, Not enough memory to connect to MySQL 
     ExitApp 
    } 

    ; figure out how to turn on reconnect call! 
    ; mysql_options(mysql, MYSQL_OPT_RECONNECT, &true); 
    value := DllCall("libmySQL.dll\mysql_options" 
      , "UInt", db 
      , "UInt", 20 ; is this the correct constant which represents MYSQL_OPT_RECONNECT?... see below 
      , "UInt", 1) ; true 

    connection := DllCall("libmySQL.dll\mysql_real_connect" 
     , "UInt", db 
     , "Str", host  ; host name 
     , "Str", user  ; user name 
     , "Str", password ; password 
     , "Str", database ; database name 
     , "UInt", 3306 ; port 
     , "UInt", 0 ; unix_socket 
     , "UInt", 0) ; client_flag 

    If (connection = 0) 
    { 
     HandleMySQLError(db, "Cannot connect to database") 
     Return 
    } 

    serverVersion := DllCall("libmySQL.dll\mysql_get_server_info", "UInt", db, "Str") 



    ;MsgBox % "Ping database: " . DllCall("libmySQL.dll\mysql_ping", "UInt", db) . "`nServer version: " . serverVersion 

    return db 

} 

;============================================================ 
; mysql error handling 
;============================================================ 

HandleMySQLError(db, message, query="") {  ; the equal sign means optional 
    errorCode := DllCall("libmySQL.dll\mysql_errno", "UInt", db) 
    errorStr := DllCall("libmySQL.dll\mysql_error", "UInt", db, "Str") 
    MsgBox 16, MySQL Error: %message%, Error %errorCode%: %errorStr%`n`n%query% 
    Return 
} 

;============================================================ 
; mysql get address 
;============================================================ 

GetUIntAtAddress(_addr, _offset) 
{ 
    local addr 

    addr := _addr + _offset * 4 

    Return *addr + (*(addr + 1) << 8) + (*(addr + 2) << 16) + (*(addr + 3) << 24) 
} 

;============================================================ 
; process query 
;============================================================ 

dbQuery(_db, _query) 
{ 
    local resultString, result, requestResult, fieldCount 
    local row, lengths, length, fieldPointer, field 

    query4error := RegExReplace(_query , "\t", " ") ; convert tabs to spaces so error message formatting is legible 
    result := DllCall("libmySQL.dll\mysql_query", "UInt", _db , "Str", _query) 

    If (result != 0) { 
     errorMsg = %_query% 
     HandleMySQLError(_db, "dbQuery Fail", query4error) 
     Return 
    } 

    requestResult := DllCall("libmySQL.dll\mysql_store_result", "UInt", _db) 

    if (requestResult = 0) { ; call must have been an insert or delete ... a select would return results to pass back 
     return 
    } 

    fieldCount := DllCall("libmySQL.dll\mysql_num_fields", "UInt", requestResult) 

    Loop 
    { 
     row := DllCall("libmySQL.dll\mysql_fetch_row", "UInt", requestResult) 
     If (row = 0 || row == "") 
      Break 

     ; Get a pointer on a table of lengths (unsigned long) 
     lengths := DllCall("libmySQL.dll\mysql_fetch_lengths" , "UInt", requestResult) 

     Loop %fieldCount% 
     { 
      length := GetUIntAtAddress(lengths, A_Index - 1) 
      fieldPointer := GetUIntAtAddress(row, A_Index - 1) 
      VarSetCapacity(field, length) 
      DllCall("lstrcpy", "Str", field, "UInt", fieldPointer) 
      resultString := resultString . field 
      If (A_Index < fieldCount) 
       resultString := resultString . "|"  ; seperator for fields 
     } 

     resultString := resultString . "`n"   ; seperator for records 

    } 

    ; remove last newline from resultString 
    resultString := RegExReplace(resultString , "`n$", "")  

    Return resultString 
} 
+0

AHHH ... 내 재 연결 문제를 해결할 수있는 다른 방법이 있습니다 ... my.ini mysql 구성 파일의 [mysql] 섹션에 "다시 연결"을 추가하고 mysql 서버를 다시 시작했습니다. 시간 초과가 발생하기 전에 잠시 기다려야이 동작을 확신 할 수 있습니다. 나는 알아낼 때 게시 할 것입니다. – panofish

답변

0

더 나은 ... mysql 연결 매개 변수를 유지하기 위해 oop 클래스를 사용 했으므로 mysql 연결 시간이 초과되고 새로운 mysql 호출이 만들어지면 자동으로 다시 연결할 수 있습니다.

0

상자 밖에서 생각하는 동안 나를 데려 갔지만 나는 결국 아주 잘 작동하는 해결책을 발견했습니다.

8 시간 후에 mysql 데이터베이스에 다시 연결하기위한 settimer 명령을 추가했다. 기본 데이터베이스 연결 시간 초과는 8 시간입니다.

이제 AHK 앱을 무기한으로 실행할 수 있으며 항상 데이터베이스에 연결됩니다!

+0

이것이 효과가 있었지만 위에 게시 한 최상의 솔루션은 아니 었습니다. – panofish

관련 문제