Jump to content

_SQLite_GetTable


Recommended Posts

Hi

$iRval = _SQLite_GetTable (-1, "SELECT * FROM persons;", $aResult, $iRows, $iColumns)
If $iRval = $SQLITE_OK Then

    _ArrayDisplay($aResult, "Query Result")
Else
    MsgBox(16, "SQLite Error: " & $iRval, _SQLite_ErrMsg ())
EndIf

This shows results like this :

;~ $aResult Looks Like this:

;~ [0] = 8

;~ [1] = Name

;~ [2] = Age

;~ [3] = Alice

;~ [4] = 43

;~ [5] = Bob

;~ [6] = 28

;~ [7] = Cindy

;~ [8] = 21

How to get results like this using _SQLite_GetTable

;~ Name Age

;~ Alice 43

;~ Bob 28

;~ Cindy 21

Edited by autoitxp
Link to comment
Share on other sites

The results are all strung together into a single 1D array. The only confusing part is doing the math to determine the array elements to get for each row and column.

Try this modification of the demo script in the help file under _SQLite_GetTable():

#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <Array.au3>

Local $aResult, $iRows, $iColumns, $iRval

_SQLite_Startup()
If @error > 0 Then
    MsgBox(16, "SQLite Error", "SQLite.dll can't be loaded!")
    Exit -1
EndIf

_SQLite_Open() ; Open a :memory: database
If @error > 0 Then
    MsgBox(16, "SQLite Error", "Can't open database!")
    Exit -1
EndIf

;Example Table
If Not _SQLite_Exec(-1, "CREATE TEMP TABLE persons (Name, Age);") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES ('Alice','43');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES ('Bob','28');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES ('Cindy','21');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())

; Query
$iRval = _SQLite_GetTable(-1, "SELECT * FROM persons;", $aResult, $iRows, $iColumns)
If $iRval = $SQLITE_OK Then
    _ArrayDisplay($aResult, "Query Result: $iRows = " & $iRows & "  $iColumns = " & $iColumns)
    
    ; Reformat results
    $sMsg = ""
    For $r = 0 To $iRows - 1
        For $c = 1 To $iColumns
            $sMsg &= $aResult[($r * $iColumns) + $c] & " "
        Next
        $sMsg &= @CRLF
    Next
    MsgBox(64, "Results", $sMsg)
Else
    MsgBox(16, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndIf

_SQLite_Close()
_SQLite_Shutdown()

:)

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

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...