MySQL Zugriff - Login Abfrage - Ablaufoptimierung

  • Hallöchen zusammen,

    ich versuche mich seit einiger Zeit an einem Tool für Zeiterfassung.
    Bisher hänge ich aber an folgendem Problem,
    ich möchte nicht jedesmal die Abfrage (Zeile 47 - 75) neu erstellen da sich jedesmal nur das $Query ändert.
    (Diese Abfrage bleibt für fast alle Tabellen im weiteren Tool gleich)

    Es ist im Hintergrund eine MySQL Datenbank angelegt.
    DB-Name: Zeiterfassung
    Table: login
    Spalten: user, pwd, info

    im Script interessiert aber nur die user und die pwd spalte.

    Das Script funktioniert soweit.
    Verbesserungsvorschläge fänd ich auch super :)

    Spoiler anzeigen
    [autoit]

    #include <array.au3>
    #include "mysql.au3"
    #include <ButtonConstants.au3>
    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Dim $User, $PWD, $IP, $Database, $query, $res, $rows, $fields

    [/autoit] [autoit][/autoit] [autoit]

    $User = "test"
    $PWD = "test"
    $IP = "192.168.177.10"
    $Database = "Zeiterfassung"

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #Region # Login GUI
    $Form1 = GUICreate("Login - Zeiterfassung", 330, 148, 310, 263)
    $Input1 = GUICtrlCreateInput("", 8, 24, 161, 21)
    $Input2 = GUICtrlCreateInput("", 8, 88, 161, 21, $ES_PASSWORD)
    $Label1 = GUICtrlCreateLabel("Benutzername", 8, 0, 70, 17)
    $Label2 = GUICtrlCreateLabel("Passwort", 8, 64, 50, 17)
    $Button1 = GUICtrlCreateButton("Anmelden", 8, 120, 65, 25, 0)
    $Button2 = GUICtrlCreateButton("Verlassen", 248, 120, 73, 25, 0)
    GUISetState(@SW_SHOW)
    #EndRegion # Login GUI

    [/autoit] [autoit][/autoit] [autoit]

    #Region # While Schleife für GUI $nMsg
    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    Case $Button2
    Exit
    Case $Button1
    Call("login")
    EndSwitch
    WEnd
    #EndRegion # While Schleife für GUI $nMsg
    Func login()

    [/autoit] [autoit][/autoit] [autoit]

    $Username = GUICtrlRead($Input1)
    $query = 'SELECT pwd FROM login where user = "' & $Username & '"'

    [/autoit] [autoit][/autoit] [autoit]

    _MySQL_InitLibrary()
    $MysqlConn = _MySQL_Init()
    $connected = _MySQL_Real_Connect($MysqlConn, $IP, $User, $PWD, $Database)

    [/autoit] [autoit][/autoit] [autoit]

    If $connected = 0 Then
    $errno = _MySQL_errno($MysqlConn)
    MsgBox(0, "Error:", $errno & @LF & _MySQL_error($MysqlConn))
    If $errno = $CR_UNKNOWN_HOST Then MsgBox(0, "Error:", "$CR_UNKNOWN_HOST" & @LF & $CR_UNKNOWN_HOST)
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    _MySQL_Real_Query($MysqlConn, $query)

    [/autoit] [autoit][/autoit] [autoit]

    $res = _MySQL_Store_Result($MysqlConn)
    $fields = _MySQL_Num_Fields($res)
    $rows = _MySQL_Num_Rows($res)
    If $rows > 0 Then
    Dim $array[$rows][$fields]
    For $k = 1 To $rows
    $mysqlrow = _MySQL_Fetch_Row($res, $fields)

    [/autoit] [autoit][/autoit] [autoit]

    $lenthsStruct = _MySQL_Fetch_Lengths($res)

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 1 To $fields
    $length = DllStructGetData($lenthsStruct, 1, $i)
    $fieldPtr = DllStructGetData($mysqlrow, 1, $i)
    $data = DllStructGetData(DllStructCreate("char[" & $length & "]", $fieldPtr), 1)
    $array[$k - 1][$i - 1] = $data
    Next
    Next

    [/autoit] [autoit][/autoit] [autoit]

    $Pass = GUICtrlRead($Input2)
    If $array[0][0] = $Pass Then
    MsgBox(0, "", "Passwort korrekt")
    Else
    MsgBox(0, "", "Passwort falsch")
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    Else
    MsgBox(0, "", "Falscher User")
    EndIf

    [/autoit] [autoit][/autoit] [autoit]

    ; Abfrage freigeben
    _MySQL_Free_Result($res)
    ; Verbindung beenden
    _MySQL_Close($MysqlConn)
    ; MYSQL beenden
    _MySQL_EndLibrary()

    [/autoit] [autoit][/autoit] [autoit]

    EndFunc ;==>login

    [/autoit]