Snippets ( Combo )

From AutoIt Wiki
Jump to navigation Jump to search

Example of a typical READ MODIFY WRITE cycle using SQLite, Array and Combo.

The code below creates a small SQLite database, queries the content, populates a Combo box, allows edit in the combo and then updates the SQLite db. Note that the SQLite rowid is stored in the array, but not displayed in the combo.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.1
 Author:         kylomas
 Edit:           Skysnake

 Script Function:
	Example AutoIt script.
	Read SQLite database to Array
	Populate Combo with Array
	Modify Combo data
	Write to SQLite db
	Note that the SQLite rowid is stored in the Array but not displayed in the combo

#ce ----------------------------------------------------------------------------

#include <GUIConstantsEx.au3>
#include <array.au3>
#include <sqlite.au3>
#include <GuiComboBox.au3>

#AutoIt3Wrapper_Add_Constants=n

_SQLite_Startup()
_SQLite_Open("sql2combo.db3")

Local $sql, $aRows, $iCols, $iRows, $LastSelected
$sql = 'create table T1 (Fname, MI, Lname);'
$sql &= 'insert into T1 values("Thomas","R","Jones");'
$sql &= 'insert into T1 values("William","X","Smith");'
$sql &= 'insert into T1 values("James","T","Kirk");'
$sql &= 'insert into T1 values("Rastus","P","Badass");'
$sql &= 'insert into T1 values("John","R","Prince");'
$sql &= 'insert into T1 values("Harry","X","Jackson");'
$sql &= 'insert into T1 values("Dick","T","Mitchell");'
$sql &= 'insert into T1 values("Tom","P","Camden");'
_SQLite_Exec(-1, $sql)

Local $gui010 = GUICreate('SQLite _rowid_ Example')
GUICtrlCreateLabel('Middle Initial', 140, 15, 100, 20)
Local $cMI = GUICtrlCreateCombo('', 140, 30, 100, 50)
Local $dummyenter = GUICtrlCreateDummy()
GUICtrlCreateLabel('Hit [ENTER] to update DB entry', 110, 70, 200, 20)

_pop_combo()

GUISetState()

Local $aAcKeys[1][2] = [["{Enter}", $dummyenter]]
GUISetAccelerators($aAcKeys)

While 1
    Switch GUIGetMsg()
        Case $gui_event_close
            Exit
        Case $cMI
			; add one to combo for SQL rowid
            $LastSelected = _GUICtrlComboBox_GetCurSel($cMI)+1
			ConsoleWrite("$LastSelected: " & $LastSelected & @CRLF)
			;Local $showSelected=$LastSelected+1
        Case $dummyenter
            If $LastSelected = '' Then ContinueLoop
            ConsoleWrite("SQL Row: " & $aRows[$LastSelected][0] & @CRLF)
            _SQLite_Exec(-1, 'update T1 set MI = ' & _SQLite_FastEscape(GUICtrlRead($cMI)) & ' where _rowid_ = ' & $aRows[$LastSelected][0] & ';')
            _pop_combo()
    EndSwitch
WEnd

Func _pop_combo()

    _SQLite_GetTable2d(-1, 'select _rowid_, MI, * from T1 order by MI limit 5', $aRows, $iRows, $iCols)

    _arraydisplay($aRows)
	ConsoleWrite(_ArrayToString($aRows) & @CRLF)

    Local $str = '|'

    For $i = 1 To UBound($aRows) - 1
        $str &= $aRows[$i][1] & '|'
    Next
    GUICtrlSetData($cMI, $str, $aRows[1][1])

    $LastSelected = ''

EndFunc   ;==>_pop_combo