Jump to content

SQL Query Results


Sticky
 Share

Recommended Posts

I've been able to successfully connect to a database and perform a query. However I do not know how to use the results I'm assuming are coming through. I've used a simple MsgBox to see if it was a string, and _ArrayDisplay if it was in array form.

Is there another method I must use with the object to get it into an array in AutoIt?

Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")

$sqlCon = ObjCreate("ADODB.Connection")
$sqlCon.ConnectionString = "driver={SQL Server};server=192.168.10.1;uid=uid;pwd=pwd"
$sqlCon.Open

; status is always 1 which means it successfully connected
$status = $sqlCon.State

; request data
$strSQL = "select * from table"
$result = $sqlCon.execute ($strSQL)

; HOW DO I DISPLAY RESULTS?
_ArrayDisplay($result)
MsgBox(0, "", $result)


$sqlCon.Close

Func MyErrFunc()

  Msgbox(0,"AutoItCOM Test","We intercepted a COM Error !"      & @CRLF  & @CRLF & _
             "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
             "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
             "err.number is: "         & @TAB & hex($oMyError.number,8)  & @CRLF & _
             "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
             "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
             "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
             "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
             "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
            )

    Local $err = $oMyError.number
    If $err = 0 Then $err = -1

    $g_eventerror = $err  ; to check for after this function returns
Endfunc
Link to comment
Share on other sites

You need to learn to use COM objects. Your $result is a "Record Set" object. Check it out at the ADODB Tutorial on W3Schools.

An example:

Global $sMsg = ""

; ... $result contains result set

For $oField In $result.Fields
    $sMsg &= $oField.Name & @TAB
Next
$sMsg &= @CRLF

While Not $result.EOF
    For $oField in $result.Fields
        $sMsg &= $oField.Value & @TAB
    next
    $sMsg &= @CRLF
    $result.MoveNext
Next
$result.Close
 
ConsoleWrite($sMsg)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You need to learn to use COM objects. Your $result is a "Record Set" object. Check it out at the ADODB Tutorial on W3Schools.

An example:

Global $sMsg = ""

; ... $result contains result set

For $oField In $result.Fields
    $sMsg &= $oField.Name & @TAB
Next
$sMsg &= @CRLF

While Not $result.EOF
    For $oField in $result.Fields
        $sMsg &= $oField.Value & @TAB
    next
    $sMsg &= @CRLF
    $result.MoveNext
Next
$result.Close
 
ConsoleWrite($sMsg)

:)

Thank you very very much! This is exactly what I needed, thank you for taking the time to go as far as taking out the column names and everything, very much appreciated. I will look into the W3 link you gave for future reference.

Link to comment
Share on other sites

  • 1 year later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...