Jump to content

Recommended Posts

Posted

Hi. i am new in autoit.

This code is when you push one of btns, it read from sqlite db file and randomly select a row then print to text edit and copy to clipboard.

Also, When one of btns selected,  pushing ctrl+b makes btn function again.

But When hotkey is pushed repeatedly, the error occurrence.

i don't understand why Hotkey works fine in first several times, then later it's not working.

This is my error msg.

==> Subscript used on non-accessible variable.:
$irandom = Random(1, $aRow[0], 1)
$irandom = Random(1, $aRow^ ERROR

And this is my code

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Inputer", 246, 203, -1, 0)
$Edit1 = GUICtrlCreateEdit("", 104, 16, 121, 161, BitOR($ES_MULTILINE,$ES_READONLY))
GUICtrlSetData($Edit1, "How to use" & @CRLF & "1. Push Btn for select a table" & @CRLF & "2. Ctrl+V for paste" & @CRLF & "3. Ctrl+B for renewal")
$Button1 = GUICtrlCreateButton("A", 16, 16, 73, 25)
$Button2 = GUICtrlCreateButton("B", 16, 48, 73, 25)
$Button3 = GUICtrlCreateButton("C", 16, 80, 73, 25)
$Button4 = GUICtrlCreateButton("D", 16, 112, 73, 25)
$Button5 = GUICtrlCreateButton("E", 16, 144, 73, 25)
$Label1 = GUICtrlCreateLabel("", 32, 176, 36, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Local $aRow, $t1, $irandom, $lbtext
Func B1()
    GUICtrlSetData($Label1, "")
    GUICtrlSetData($Edit1, "")
    GUICtrlSetData($Label1, "A")
    _SQLite_QuerySingleRow(-1, "SELECT max(rowid) FROM a", $aRow)
    $irandom = Random(1, $aRow[0], 1)
    _SQLite_QuerySingleRow(-1, "SELECT * FROM a WHERE rowid = " & $irandom, $t1)
    GUICtrlSetData($Edit1, $t1[0])
    ClipPut($t1[0] & @CRLF)
EndFunc
Func B2()
    GUICtrlSetData($Label1, "")
    GUICtrlSetData($Edit1, "")
    GUICtrlSetData($Label1, "B")
    _SQLite_QuerySingleRow(-1, "SELECT max(rowid) FROM b", $aRow)
    $irandom = Random(1, $aRow[0], 1)
    _SQLite_QuerySingleRow(-1, "SELECT * FROM b WHERE rowid = " & $irandom, $t1)
    GUICtrlSetData($Edit1, $t1[0])
    ClipPut($t1[0] & @CRLF)
EndFunc
Func B3()
    GUICtrlSetData($Label1, "")
    GUICtrlSetData($Edit1, "")
    GUICtrlSetData($Label1, "C")
    _SQLite_QuerySingleRow(-1, "SELECT max(rowid) FROM c", $aRow)
    $irandom = Random(1, $aRow[0], 1)
    _SQLite_QuerySingleRow(-1, "SELECT * FROM c WHERE rowid = " & $irandom, $t1)
    GUICtrlSetData($Edit1, $t1[0])
    ClipPut($t1[0] & @CRLF)
EndFunc
Func B4()
    GUICtrlSetData($Label1, "")
    GUICtrlSetData($Edit1, "")
    GUICtrlSetData($Label1, "D")
    _SQLite_QuerySingleRow(-1, "SELECT max(rowid) FROM d", $aRow)
    $irandom = Random(1, $aRow[0], 1)
    _SQLite_QuerySingleRow(-1, "SELECT * FROM d WHERE rowid = " & $irandom, $t1)
    GUICtrlSetData($Edit1, $t1[0])
    ClipPut($t1[0] & @CRLF)
EndFunc
Func B5()
    GUICtrlSetData($Label1, "")
    GUICtrlSetData($Edit1, "")
    GUICtrlSetData($Label1, "E")
    _SQLite_QuerySingleRow(-1, "SELECT max(rowid) FROM e", $aRow)
    $irandom = Random(1, $aRow[0], 1)
    _SQLite_QuerySingleRow(-1, "SELECT * FROM e WHERE rowid = " & $irandom, $t1)
    GUICtrlSetData($Edit1, $t1[0])
    ClipPut($t1[0] & @CRLF)
EndFunc
HotKeySet("^b", "Kt")
Func Kt()
    $lbtext = GUICtrlRead($Label1)
    If $lbtext == "A" Then
        B1()
    ElseIf $lbtext == "B" Then
        B2()
    ElseIf $lbtext == "C" Then
        B3()
    ElseIf $lbtext == "D" Then
        B4()
    ElseIf $lbtext == "E" Then
        B5()
    EndIf
EndFunc

While 1
    $nMsg = GUIGetMsg()
    _SQLite_Startup()
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite3.dll Can't be Loaded!")
    EndIf
    Local $hDskDB = _SQLite_Open('sample.db')

    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            B1()
        Case $Button2
            B2()
        Case $Button3
            B3()
        Case $Button4
            B4()
        Case $Button5
            B5()

    EndSwitch
WEnd

Also i attach the code and db file.

plz help me

sample.au3 sample.db

Posted

And if i change a hotkey to ctrl+v from ctrl+b, it just renew a text not paste from clipboard. If i add "send("^v")" code in hotkey function, then loop makes. How can i make paste and renew altogether?

Posted

When you're writing a program in AutoIt, you need to read the documentation and check for @error(s). You're probably getting an error with one of the _SQLite_QuerySingleRow lines and the value returned in $aRow isn't an array, so there isn't an item at index 0.

When checking the error value, I like to print it to the console so I can look it up in the help file to determine what went wrong

_SQLite_QuerySingleRow(-1, "SELECT max(rowid) FROM e", $aRow)
If @error Then
    ; Do error stuff (consider exiting or handling the error)
    ConsoleWrite("_SQLite_QuerySingleRow - Error: " & @error)
Else
    ; Do anything else that relies on the result
    $iRandom = Random(1, $aRow[0], 1)
EndIf

 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Posted (edited)

Fixed and simplified code:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
#Region ### START Koda GUI section ### Form=
Global $Form1 = GUICreate("Inputer", 246, 203, -1, 0)
Global $Edit1 = GUICtrlCreateEdit("", 104, 16, 121, 161, BitOR($ES_MULTILINE,$ES_READONLY))
; leave button declaration in this order without anything in between
Global $Button1 = GUICtrlCreateButton("A", 16, 16, 73, 25)
Global $Button2 = GUICtrlCreateButton("B", 16, 48, 73, 25)
Global $Button3 = GUICtrlCreateButton("C", 16, 80, 73, 25)
Global $Button4 = GUICtrlCreateButton("D", 16, 112, 73, 25)
Global $Button5 = GUICtrlCreateButton("E", 16, 144, 73, 25)
Global $Label1 = GUICtrlCreateLabel("", 32, 176, 36, 17)
GUICtrlSetData($Edit1, "How to use" & @CRLF & "1. Push Btn for select a table" & @CRLF & "2. Ctrl+V for paste" & @CRLF & "3. Ctrl+B for renewal")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

; Init sqlite
_SQLite_Startup("C:\SQLite\bin\sqlite3.dll", False, 1) ;<-- Change to the location of your sqlite dll
If @error Then
    MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite3.dll Can't be Loaded!")
EndIf
_SQLite_Open('sample.db')
Global $nMsg

HotKeySet("^b", "Kt")

While 1
    Local $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1 To $Button5   ; assuming 5 buttons are declared in immediate succession
            B1_5(1)
    EndSwitch
WEnd

Func B1_5($iReason)
    Local $aRow, $t1, $lbtext, $sLetter
    If $iReason Then
        $sLetter = Chr(Asc("A") + $nMsg - $Button1) ; assuming 5 buttons are declared in immediate succession
    Else
        $sLetter = GUICtrlRead($Label1)
    EndIf
    _SQLite_QuerySingleRow(-1, "SELECT * FROM " & $sLetter & " order by random() limit 1", $t1) ; beware of DB empty!
    If $iReason Then GUICtrlSetData($Label1, $sLetter)
    GUICtrlSetData($Edit1, $t1[0])
    ClipPut($t1[0] & @CRLF)
EndFunc

Func Kt()
    B1_5(0)
EndFunc

But I don't understand what you mean by

1 hour ago, jogae012 said:

How can i make paste and renew altogether?

Paste what and where?

If you define a hotkey for ^v the default meaning of ^v (paste clipboard at the current carret position) is overriden.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Posted

@jchd Thanks for your working. In fact, my original code's button has different name is not a alphabetical but korean. But i use your code and it works perfectly. 

And i would ask to another thread for other my question. 

 

Posted (edited)

You can type korean codepoints (and much more) as well provided your .AU3 source file is UTF8 encoded:

Local $s = _
    "FR: Restez chez vous !" & @LF & _
    "EN: Stay home!" & @LF & _
    "KR: 집에있어 라!" & @LF & _
    "GR: Μείνε σπίτι!" & @LF & _
    "IN: घरी रहा!" & @LF & _
    "JP: 家にいる!" & @LF & _
    "RU: Остаться дома!" & @LF & _
    "LA: ຢູ່ບ້ານ!" & @LF & _
    "TJ: Дар хона бимонед!" & @LF & _
    "BD: বাড়িতে থাকুন!" & @LF & _
    "EG: ابق في المنزل!" & @LF & _
    "TH: อยู่ที่บ้าน!" & @LF & _
    "IL: בלייַבן אין שטוב!" & @LF
MsgBox(0, "Babel", $s)

SQLite supports Unicode too, so nothing to change there.

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...