geänderten array wert auf datenbank updaten

  • es geht dadrum, das wenn ein wert in der listview geändert wird, das dieser dann upgedatet wird wenn man auf den Button klickt.

    Spoiler anzeigen
    [autoit]


    #include <array.au3>
    #include "mysql.au3"
    #include <GUIConstantsEx.au3>
    #include <GUIConstants.au3>
    #include <EditConstants.au3>
    #include <GuiListView.au3>
    #include <ListviewConstants.au3>
    #include <WindowsConstants.au3>
    #include<StructureConstants.au3>
    #include<StaticConstants.au3>

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

    Global $currentItem[2], $title, $currentOpt[2]

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

    ; MYSQL starten, DLL im PATH (enthält auch @ScriptDir), sont Pfad zur DLL angeben. DLL muss libmysql.dll heißen.
    _MySQL_InitLibrary()
    If @error Then Exit MsgBox(0, '', "")

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

    $MysqlConn = _MySQL_Init()
    ;$PW = InputBox("Logindaten", "Gib hier das Password ein", "", "*")
    $PW = "root"
    $connected = _MySQL_Real_Connect($MysqlConn, "localhost", "cash", $PW , "chipsy")

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

    If $connected = 0 Then Exit MsgBox(16, 'Connection Error', _MySQL_Error($MysqlConn))

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

    $query = "select vorgang.id, art.typ, gegenstand.Autor, gegenstand.titel,person.vorname, person.nachname, vorgang.verleihdatum, vorgang.rueckgabedatum FROM vorgang, art, gegenstand, person where gegenstand.id = vorgang.gegenstand_id and art.id = vorgang.art_id and person.id = vorgang.person_id order by vorgang.id"
    ;$query2 = "update person set vorname = 'gustav' where Vorname = 'test'"

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

    ;-- and vorgang.rueckgabedatum is not null;
    ;-- and gegenstand.autor is not null
    ;-- and art.typ = 'buch'

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

    _MySQL_Real_Query($MysqlConn, $query)
    $res = _MySQL_Store_Result($MysqlConn)
    $fields = _MySQL_Num_Fields($res)
    $rows = _MySQL_Num_Rows($res)
    ;MsgBox(0, "", "Rows: "&$rows & "-" & "Fields: "& $fields)

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

    $array = _MySQL_Fetch_Result_StringArray($res)

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

    $GUI = GUICreate("Listview Einträge", 600, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    $button1 = GUICtrlCreateButton("Ändern", 75, 170, 70, 20)
    GUISetBkColor(0x00E0FFFF) ; Ändert die Hintergrundfarbe
    $listview = GUICtrlCreateListView($array[0][0] &"|"& $array[0][1] &"|"& $array[0][2] &"|"& $array[0][3] &"|"& $array[0][4] &"|"& $array[0][5] &"|"& $array[0][6] &"|"& $array[0][7], 10, 10, 580, 150);,$LVS_SORTDESCENDING)

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

    for $i = 1 to UBound($array) -1
    $item1 = GUICtrlCreateListViewItem($array[$i][0] &"|"& $array[$i][1] &"|"& $array[$i][2] &"|"& $array[$i][3] &"|"& $array[$i][4] &"|"& $array[$i][5] &"|"& $array[$i][6] &"|"& $array[$i][7], $listview)
    ;$item2 = GUICtrlCreateListViewItem($array[$i][1], $listview)
    ;MsgBox(0,"",$array[$i])
    _GUICtrlListView_EditLabel($listview,0)
    next

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

    $EditBox = GUICreate('', 200, 30, -1, -1, BitOR($WS_POPUP,$WS_BORDER), $WS_EX_TOPMOST)
    $hEdit = GUICtrlCreateInput('', 5, 5, 190, 20, $SS_CENTER)
    HotKeySet('{ESC}', '_EscEdit')
    GUISetState(@SW_SHOW, $GUI)
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

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

    While True
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    If $msg = $button1 then
    ;überprüfen ob etwas geändert wurde

    ;Update in die Datenbank mit den neuen Werten
    MsgBox(0,"","test")
    $query2 = "update person set vorname = 'test' where Vorname = 'gustav'"
    _MySQL_Real_Query($MysqlConn, $query2)
    $res2 = _MySQL_Store_Result($MysqlConn)
    EndIf

    WEnd

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

    $input1 = GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; Erlaubt drag&drop
    GUISetState()

    #cs
    Do
    $msg = GUIGetMsg()

    Select
    Case $msg = $button1
    ;Update in die Datenbank mit den neuen Werten
    MsgBox(0,"","test")
    $query2 = "update person set vorname = 'test' where Vorname = 'gustav'"
    _MySQL_Real_Query($MysqlConn, $query2)
    $res2 = _MySQL_Store_Result($MysqlConn)
    Case $GUI_EVENT_CLOSE
    Exit

    EndSelect
    Until $msg = $GUI_EVENT_CLOSE
    #ce

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

    _MySQL_Free_Result($res)
    _MySQL_Close($MysqlConn)
    _MySQL_ShutDown()

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

    Func _LeftDblClick($Info)
    If $Info[3] = -1 Then Return
    $currentOpt[0] = Opt('GUICoordMode', 1)
    $currentOpt[1] = Opt("GUIOnEventMode", 1)
    GUICtrlSetOnEvent($hEdit, '_EditEnter')
    _GUICtrlListView_SetSelectedColumn($Info[1], $Info[4])
    $title = WinGetTitle($GUI)
    Local $mouse = MouseGetPos(), $posEdit = WinGetPos($EditBox)
    Local $posGUI = WinGetPos($GUI), $posLV = ControlGetPos($GUI, '', $listview)
    Local $colInfo = _GUICtrlListView_GetColumn($Info[1], $Info[4]), $sumWidth = 0, $tmp
    WinSetTitle($GUI, '', 'Editiere: Zeile ' & $Info[3]+1 & ', ' & $colInfo[5])
    For $i = 0 To $Info[4]
    $tmp = _GUICtrlListView_GetColumn($Info[1], $i)
    $sumWidth += $tmp[4]
    Next
    $sumWidth -= $colInfo[4]
    Local $xPos = $posGUI[0]+$posLV[0]+$sumWidth
    If $xPos+$colInfo[4]+10 > @DesktopWidth Then $xPos = @DesktopWidth - ($colInfo[4] + 10)
    If $xPos < 0 Then $xPos = 0
    WinMove($EditBox, '', $xPos, $mouse[1]-$posEdit[3], $colInfo[4]+10)
    GUICtrlSetData($hEdit, _GUICtrlListView_GetItemText($Info[1], $Info[3], $Info[4]))
    $currentItem[0] = $Info[3]
    $currentItem[1] = $Info[4]
    GUISetState(@SW_SHOW, $EditBox)
    EndFunc

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

    Func _EscEdit()
    WinSetTitle($GUI, '', $title)
    Opt('GUICoordMode', $currentOpt[0])
    Opt("GUIOnEventMode", $currentOpt[1])
    GUISetState(@SW_HIDE, $EditBox)
    EndFunc

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

    Func _EditEnter()
    WinSetTitle($GUI, '', $title)
    Opt('GUICoordMode', $currentOpt[0])
    Opt("GUIOnEventMode", $currentOpt[1])
    _GUICtrlListView_SetItemText($listview, $currentItem[0], GUICtrlRead($hEdit), $currentItem[1])
    GUISetState(@SW_HIDE, $EditBox)
    EndFunc

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

    Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $listview ; ID des LV
    If Not IsHWnd($listview) Then $hWndListView = GUICtrlGetHandle($listview) ; ID des LV
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
    Case $hWndListView
    Switch $iCode
    Case $NM_DBLCLK
    Local $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam)
    Local $aInfo[5] = [$hWndFrom, $iIDFrom, $iCode, DllStructGetData($tInfo, "Index"), _
    DllStructGetData($tInfo, "SubItem")]
    _LeftDblClick($aInfo)
    EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
    EndFunc ;==>WM_NOTIFY

    [/autoit]
  • Naja speicher doch beim ersten mal Button klick die selected indices in nen array (_GUICtrlListView_GetSelectedIndices) und dann vergleichst du einfach beim nächsten klick das Array mit den aktuell ausgewählten und schreibst ggf. in die DB

  • Danke Milla, hab zwar nicht _GUICtrlListView_GetSelectedIndices verwendet, aber so wie ichs gemacht hab funktioniert ganz gut! hast nur du mich drauf gebracht :D

    hier der code für die anderen

    also beim drücken auf den button wird der gerade geändert wert in $sText abgespeichert, den kann ich dann vergleichen, wenn sich was geändert hat wird ein update abgesetzt ;)

    Spoiler anzeigen
    [autoit]

    While True
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit
    If $msg = $button1 then
    Local $aItem ="", $sText = ""
    ;überprüfen ob etwas geändert wurde
    $aItem = _GUICtrlListView_GetItemTextArray($listview, $currentItem[0])
    $aItem2 = _GUICtrlListView_GetItemTextArray($listview, $currentItem[1])
    for $i = 1 To 1
    $sText &= StringFormat("spalte[%2d] zeile[%2d] %s", $currentItem[0], $currentItem[1], $aItem[$currentItem[1]+1]) & @LF
    Next
    MsgBox(4160, "Information", "" & @LF & @LF & $sText)

    [/autoit]