Jump to content

SQLiteEx UDF v0.5.1 - Simplest work with SQLite


57ar7up
 Share

Recommended Posts

SQLiteEx v0.5.1

Solved some bugs and mistakes

 

List of public functions

 

_SQLiteEx_Open

_SQLiteEx_Get

_SQLiteEx_Set

_SQLiteEx_Insert

_SQLiteEx_Delete

_SQLiteEx_QuerySingleRow

_SQLiteEx_FirstTableEntry

_SQLiteEx_LastTableEntry

_SQLiteEx_SetTable

_SQLiteEx_TableExists

_SQLiteEx_ShowTable

_SQLiteEx_DropTable

_SQLiteEx_DatabaseExists

_SQLiteEx_DropDatabase

_SQLiteEx_Close

 

Full code

#Region Header

#cs
    Title:          SQLite Extending Library for AutoIt3
    Filename:       SQLiteEx.au3
    Description:    Set of useful SQLite functions
    Author:         57ar7up
    Version:        0.5.1
    Last Update:    21/01/14
    Requirements:   AutoIt v3.3 +, Developed/Tested on Windows 7
    Notes:          With this UDF you can work only with one database simultaneously, for simplicity

    Available functions:

    _SQLiteEx_Open
    _SQLiteEx_Get
    _SQLiteEx_Set
    _SQLiteEx_Insert
    _SQLiteEx_Delete
    _SQLiteEx_QuerySingleRow
    _SQLiteEx_FirstTableEntry
    _SQLiteEx_LastTableEntry
    _SQLiteEx_SetTable
    _SQLiteEx_TableExists
    _SQLiteEx_ShowTable
    _SQLiteEx_DropTable
    _SQLiteEx_DatabaseExists
    _SQLiteEx_DropDatabase
    _SQLiteEx_Close

    Examples: see SQLiteEx_Examples.au3
#ce

#Include-once
#Include 'SQLite.au3'
#Include 'SQLite.dll.au3'

#EndRegion Header

#Region Global Variables and Constants

Global $_sDBExtension   = '.sqlite3'        ;Database extension
Global $_sDBsPath       = @ScriptDir & '/'  ;Path to databases
Global $_hDB            = FALSE             ;Current database handle
Global $_sDBName        = FALSE             ;Current database name
Global $_sTable         = FALSE             ;Current table name

Dim $aSQLiteErrors[24][2] = [ _
    [0, 'OK'], _
    [1, 'SQL error or missing database'], _
    [2, 'An internal logic error in SQLite'], _
    [3, 'Access permission denied'], _
    [4, 'Callback routine requested an abort'], _
    [5, 'The database file is locked'], _
    [6, 'A table in the database is locked'], _
    [7, 'A malloc() failed'], _
    [8, 'Attempt to write a readonly database'], _
    [9, 'Operation terminated by sqlite_interrupt()'], _
    [10, 'Some kind of disk I/O error occurred'], _
    [11, 'The database disk image is malformed'], _
    [12, '(Internal Only) Table or record not found'], _
    [13, 'Insertion failed because database is full'], _
    [14, 'Unable to open the database file'], _
    [15, 'Database lock protocol error'], _
    [16, '(Internal Only) Database table is empty'], _
    [17, 'The database schema changed'], _
    [18, 'Too much data for one row of a table'], _
    [19, 'Abort due to constraint violation'], _
    [20, 'Data type mismatch'], _
    [21, 'Library used incorrectly'], _
    [22, 'Uses OS features not supported on host'], _
    [23, 'Authorization denied'] _
]

#EndRegion Global Variables and Constants

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_Open
; Description....: Opens SQLite database
; Syntax.........: _SQLite_Open ($sDBWay [, $sSQL])
;                  $sDBWay  - Way to name new DB or find DB file, can be file name in database folder with extension or without (must be $_sDBExtension)
;                  $sSQL    - SQL code to execute, if provided
; Return values..: Success  - TRUE
;                  Failure  - FALSE
; Author.........: 57ar7up
; Remarks........: If not exists, new DB created
; ======

Func _SQLiteEx_Open($sDBWay = FALSE, $sSQL = FALSE) ;Returns handle to DB
    _SQLite_Startup()
    If @error > 0 Then
        Exit - 1
    EndIf
    If Not $sDBWay = FALSE Then
        $sDBPath = $_sDBsPath & $sDBWay
        If Not FileExists($sDBPath) Then
            Local $sDrive, $sDir, $sFileName, $sExtension
            Local $aPathSplit = _PathSplit($sDBPath, $sDrive, $sDir, $sFileName, $sExtension)
            If $sExtension = $_sDBExtension Then
                _FileCreate($sDBPath)
            Else
                $sDBPath &= $_sDBExtension
                If NOT FileExists($sDBPath) Then _FileCreate($sDBPath)
            EndIf
        EndIf
    Else
        $sTempFile = _TempFile(@ScriptFullPath, '', $_sDBExtension)
        Local $sDrive, $sDir, $sFileName, $sExtension
        Local $aPathSplit = _PathSplit($sTempFile, $sDrive, $sDir, $sFileName, $sExtension)
        $sDBPath = $sFileName & $sExtension
    EndIf
    $_hDB = _SQLite_Open($sDBPath)
    If $_hDB <> FALSE Then
        $_sDBName = __FileBasename($sDBPath)
        
        If $sSQL <> FALSE Then
            $iSQLite_Status = _SQLite_Exec($_hDB, $sSQL)
            If Not $iSQLite_Status = $SQLite_OK Then
                Return SetError(1, FALSE, _SQLite_ErrMsg());$aSQLiteErrors[$iSQLite_Status][1])
            Else
                Return TRUE
            EndIf
        Else
            Return TRUE
        EndIf
    Else
        Say('Failed to open DB')
        Sleep(2000)
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_Get
; Description....: Gets value from specified column
; Syntax.........: _SQLiteEx_Get ($sColumn [, $sWhere])
;                  $sColumn - Name of the existing column in current working table
;                  $sWhere  - Optional SQL code inserted after 'WHERE '
; Return values..: Success  - Value
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_Get($sColumn, $sWhere = FALSE)
    If $_sTable = FALSE Then Return FALSE
    If $sWhere Then
        $sSQL = "SELECT " & $sColumn & " FROM " & $_sTable & " WHERE " & $sWhere
    Else
        $sSQL = "SELECT " & $sColumn & " FROM " & $_sTable
    EndIf
    $aRow = _SQLiteEx_QuerySingleRow($sSQL)
    If Not IsArray($aRow) Then
        Return FALSE
    Else
        Return $aRow[0]
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_Set
; Description....: Sets value for specified column
; Syntax.........: _SQLiteEx_Set ($Field, $Value [, $sWhere])
;                  $sField  - Name of the existing column in current working table
;                  $sValue  - Data to set
;                  $sWhere  - Optional SQL code inserted after 'WHERE'
; Return values..: Success  - TRUE
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_Set($Field, $Value, $sWhere = Default)
    If $_sTable = FALSE Then Return FALSE
    If $sWhere <> Default Then
        $sWhereIs = ' WHERE ' & $sWhere
    Else
        $sWhereIs = ''
    EndIf
    $sSQL = 'SELECT * FROM ' & $_sTable & $sWhereIs
    $aRow = _SQLiteEx_QuerySingleRow($sSQL)
    If IsArray($aRow) Then
        If Not __SQLiteEx_Update($Field, $Value, $sWhere) = FALSE Then
            Return TRUE
        Else
            Return FALSE
        EndIf
    Else
        If Not _SQLiteEx_Insert($Field, $Value) = FALSE Then
            Return TRUE
        Else
            Return FALSE
        EndIf
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_Insert
; Description....: Inserts row with value at specified column
; Syntax.........: _SQLiteEx_Insert ($Field, $Value)
;                  $sField  - Name of the existing column in current working table
;                  $sValue  - Data to insert
; Return values..: Success  - Id of inserted row
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_Insert($Field, $Value) ;Returns inserted id if success
    If IsArray($Field) Then
        Local $sField, $sValue = ''
        For $i = 0 To UBound($Field) - 1
            $sField &= $Field[$i]
            $sValue &= $Value[$i]
            If $i <> UBound($Field) - 1 Then
                $sField &= "', '"
                $sValue &= "', '"
            EndIf
        Next
    Else
        $sField = $Field
        $sValue = $Value
    EndIf
    If $_sTable = FALSE Then Return FALSE
    $sSQL = "INSERT INTO " & $_sTable & " ('" & $sField & "') VALUES ('" & $sValue & "');"
    If _SQLite_Exec(-1, $sSQL) = $SQLite_OK Then
        Return _SQLite_LastInsertRowID()
    Else
        Return SetError(_SQLite_ErrMsg(), '', FALSE)
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_Delete
; Description....: Deletes row with value at specified column
; Syntax.........: _SQLiteEx_Delete ( $sField [, $sValue])
;                  $sField  - Name of the existing column in current working table
;                  $sValue  - When this value at column, row removed
; Return values..: Success  - TRUE
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_Delete($sField, $sValue)
    If $_sTable = FALSE Then Return FALSE
    $sSQL = "SELECT * FROM " & $_sTable & " WHERE " & $sField & "='" & $sValue & "'"
    If _SQLiteEx_QuerySingleRow($sSQL) = FALSE Then Return FALSE
    $sSQL = "DELETE FROM " & $_sTable & " WHERE " & $sField & "='" & $sValue & "'"
    If _SQLite_Exec(-1, $sSQL) = $SQLite_OK Then
        Return TRUE
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_QuerySingleRow
; Description....: Read out the first row of the result from the specified query.
; Syntax.........: _SQLiteEx_QuerySingleRow ($sSQL)
;                  $sSQL    - SQL code
; Return values..: Success  - Array with row data
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_QuerySingleRow($sSQL)
    Local $aRet
    _SQLite_QuerySingleRow(-1, $sSQL, $aRet)
    If IsArray($aRet) Then
        Return $aRet
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_FirstTableEntry
; Description....: Gets entry with minimum RowID
; Syntax.........: _SQLiteEx_FirstTableEntry ()
; Return values..: Success - Array with row data
;                  Failure - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_FirstTableEntry()
    If $_sTable = FALSE Then Return FALSE
    $sSQL = 'SELECT * FROM ' & $_sTable & ' WHERE ROWID=(SELECT MIN(ROWID) FROM ' & $_sTable & ')';
    $aRet = _SQLiteEx_QuerySingleRow($sSQL)
    If IsArray($aRet) Then
        Return $aRet
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_LastTableEntry
; Description....: Get entry with maximum RowID
; Syntax.........: _SQLiteEx_LastTableEntry ()
; Return values..: Success - Array with row data
;                  Failure - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_LastTableEntry()
    Local $aRet
    If $_sTable = FALSE Then Return FALSE
    $sSQL = 'SELECT * FROM ' & $_sTable & ' WHERE ROWID=(SELECT MAX(ROWID) FROM ' & $_sTable & ')';
    $aRet = _SQLiteEx_QuerySingleRow($sSQL)
    If IsArray($aRet) Then
        Return $aRet
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_SetTable
; Description....: Sets current table to work with
; Syntax.........: _SQLiteEx_SetTable ($sTBL)
;                  $sTBL - Name of the table
; Return values..: none
; Author.........: 57ar7up
; ======

Func _SQLiteEx_SetTable($sTBL)
    $_sTable = $sTBL
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_TableExists
; Description....: Checks whether table with specified name exists, if not defined checks current table
; Syntax.........: _SQLiteEx_TableExists ([$sTBL])
;                  $sTBL - Name of the table
; Return values..: Success - TRUE
;                  Failure - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_TableExists($sTBL = Default)
    If $sTBL = Default Then
        If $_sTable = FALSE Then
            Return FALSE
        Else
            $sTable = $_sTable
        EndIf
    Else
        $sTable = $sTBL
    EndIf
    $sSQL = "SELECT name FROM SQLite_master WHERE type='table' AND name='" & $_sTable & "';"
    $aRet = _SQLiteEx_QuerySingleRow($sSQL)
    If $aRet[0] <> '' Then
        Return TRUE
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_ShowTable
; Description....: Checks whether table with specified name exists, if not defined checks current table
; Syntax.........: _SQLiteEx_ShowTable ([$sTBL] [, $sColumn [, $sSortType]])
;                  $sTBL        - Name of the table, if not specified, will take current
;                  $sColumn     - Name of column to sort
;                  $sSortType   - Type of sorting, ASC or DESC
; Return values..: Success      - Shows modal window with table data
;                  Failure      - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_ShowTable($sTBL = Default, $sColumn = 'RowID', $sSortType = 'ASC') ;Displays current table
    If $sTBL = Default Then
        If $_sTable = FALSE Then
            Return FALSE
        Else
            $sTable = $_sTable
        EndIf
    Else
        $sTable = $sTBL
    EndIf
    If $_sTable = FALSE Then Return FALSE
    Local $aResult, $iRows, $iColumns, $iRval
    $sSQL = 'SELECT * FROM ' & $_sTable & ' ORDER BY ' & $sColumn & ' ' & $sSortType
    $iRval = _SQLite_GetTable2d(-1, $sSQL, $aResult, $iRows, $iColumns)
    If $iRval = $SQLite_OK Then
        _ArrayDisplay($aResult, $_sTable)
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_DropTable
; Description....: Deletes table
; Syntax.........: _SQLiteEx_DropTable ($sTBL)
;                  $sTBL    - Name of the table to delete, if not specified, current table will be deleted
; Return values..: Success  - TRUE
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_DropTable($sTBL = Default)
    If $sTBL = Default Then
        If $_sTable = FALSE Then
            Return FALSE
        Else
            $sTable = $_sTable
        EndIf
    Else
        $sTable = $sTBL
    EndIf
    $sSQL = ('DROP TABLE ' & $_sTable)
    If _SQLite_Exec($_hDB, $sSQL) = $SQLite_OK Then
        Return TRUE
    Else
        Return FALSE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_DatabaseExists
; Description....: Checks whether database exists, name can be file name with extension or not
; Syntax.........: _SQLiteEx_DatabaseExists ( $sDBName )
;                  $sDB - Name of database to delete
; Return values..: Success - TRUE
;                  Failure - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_DatabaseExists($sDBName)
    If Not FileExists($sDBName) Then
        $sDBName &= $_sDBExtension
        If Not FileExists($sDBName) Then Return FALSE
    EndIf
    Return TRUE
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_DropDatabase
; Description....: Deletes database
; Syntax.........: _SQLiteEx_DropDatabase ( $sDB)
;                  $sDBN    - Name of database to delete, if not specified, current DB will be deleted
; Return values..: Success  - TRUE
;                  Failure  - FALSE
; Author.........: 57ar7up
; ======

Func _SQLiteEx_DropDatabase($sDBN = Default)
    If $sDBN = Default Then
        If $_sDBName = FALSE Then
            Return FALSE
        Else
            $sDBName = $_sDBName
        EndIf
    Else
        $sDBName = $sDBN
    EndIf
    If $_sDBName <> FALSE And $sDBName = $_sDBName Then
        _SQLite_Close($_hDB)
        _SQLite_Shutdown()
    EndIf
    $sDBPath = $_sDBsPath & $sDBName & $_sDBExtension
    If FileExists($sDBPath) Then
        $iDelete = FileDelete($sDBPath)
        If $iDelete = 1 Then Return TRUE
        Return FALSE
    Else
        Return TRUE
    EndIf
EndFunc

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLiteEx_Close
; Description....: Closes current database
; Syntax.........: _SQLiteEx_Close ()
; Return values..: none
; Author.........: 57ar7up
; ======

Func _SQLiteEx_Close()
    If $_hDB = FALSE Then $_hDB = -1
    _SQLite_Close($_hDB)
EndFunc

#EndRegion Public Functions

Func __SQLiteEx_Query($sSQL)
    Local $hQuery
    _SQLite_Query(-1, $sSQL, $hQuery)
    Return $hQuery
EndFunc

Func __SQLiteEx_Update($Field, $Value, $sWhere = Default)
    If IsArray($Field) Then
        Local $sSet
        For $i = 0 To UBound($Field) - 1
            $sSet &= $Field[$i] & " = '" & $Value[$i] & "'"
            If $i <> UBound($Field) - 1 Then $sSet &= ", "
        Next
    Else
        $sSet = $Field & " = '" & $Value & "'"
    EndIf
    If $sWhere <> Default Then
        $sWhereIs = ' WHERE ' & $sWhere
    Else
        $sWhereIs = ''
    EndIf
    If $_sTable = FALSE Then Return FALSE
    $sSQL = "UPDATE " & $_sTable & " SET " & $sSet & $sWhereIs
    If _SQLite_Exec(-1, $sSQL) = $SQLite_OK Then
        Return TRUE
    Else
        Return SetError(_SQLite_ErrMsg(), '', FALSE)
    EndIf
EndFunc

Func __FileBasename($sFile)
    Return StringRegExpReplace($sFile, "(.*?[\\/])*(.*?)((?:\.\w+\z|\z))", "$2")
EndFunc

OnAutoitExitRegister('_SQLite_Shutdown')
Examples of using

;--------------------------------------------------------------------------------------------------
;Examples of work with SQLiteEx - simple SQLite interface for AutoIt
;--------------------------------------------------------------------------------------------------


#Include 'SQLiteEx.au3'

$bVerbose = TRUE

;----------------------FIRST DATABASE--------------------------------------------------------------
$sDB_Earth          = 'earth_2012'
$sTableCountries    = 'countries'

$sSQL =  "CREATE TABLE " & $sTableCountries & " (country text, internet_users integer, population integer);" & @CR
$sSQL &= "INSERT INTO " & $sTableCountries & " (country, internet_users, population) VALUES " & @CR
$sSQL &= "('Brazil',        99357737,   193946886),"    & @CR
$sSQL &= "('China',         568192066,  1343239923),"   & @CR
$sSQL &= "('India',         151598994,  1205073612),"   & @CR
$sSQL &= "('Japan',         100684474,  127368088),"    & @CR
$sSQL &= "('Russia',        75926004,   142517670),"    & @CR
$sSQL &= "('United States', 254295536,  313847465),"    & @CR
$sSQL &= "('You are here',  2405518376, 7017846922)"    & @CR

Say('Deleting previous ' & $sDB_Earth & ' database: ' & _SQLiteEx_DropDatabase($sDB_Earth))

Say('Opening database ' & $sDB_Earth & ': ' & _SQLiteEx_Open($sDB_Earth, $sSQL))

_SQLiteEx_SetTable($sTableCountries)

Say('Set changed value: ' & _SQLiteEx_Set('country', 'World', "country = 'You are here'"))

Say('Getting value - All internet users in the World: ' & _SQLiteEx_Get('internet_users', "country = 'World'"))

Dim $aColumns[3] =  ['country',         'internet_users',   'population']
Dim $aValues[3] =   ['Ancient Rome',    0,                  0]
Say('Inserting row at Row ID: ' & _SQLiteEx_Insert($aColumns, $aValues))

Say('Deleting row: ' & _SQLiteEx_Delete('country', 'Ancient Rome'))

Say('Is table still exists: ' & _SQLiteEx_TableExists())

_SQLiteEx_ShowTable(Default, 'internet_users', 'DESC')

Say('Dropping table: ' & _SQLiteEx_DropTable($sTableCountries))

Say('Is table still exists: ' & _SQLiteEx_TableExists())

_SQLiteEx_Close()


;----------------------SECOND DATABASE-------------------------------------------------------------
$sDB_SolarSystem    = 'solar_system'
$sTablePlanets      = 'planets'

$sSQL2 =  "CREATE TABLE " & $sTablePlanets & " (planet text, mass real, length_of_day real, mean_temperature real, ring_system integer);" & @CR
$sSQL2 &= "INSERT INTO " & $sTablePlanets & " (planet, mass, length_of_day, mean_temperature, ring_system) VALUES " & @CR
$sSQL2 &= "('Mercury',  0.330,  4222.6, 167,    0)," & @CR
$sSQL2 &= "('Venus',    4.87,   2802.0, 464,    0)," & @CR
$sSQL2 &= "('Earth',    5.97,   24.0,   15,     0)," & @CR
$sSQL2 &= "('Moon',     0.073,  708.7,  -20,    0)," & @CR
$sSQL2 &= "('Mars',     0.642,  24.7,   -65,    0)," & @CR
$sSQL2 &= "('Jupiter',  1898,   9.9,    -110,   1),"  & @CR
$sSQL2 &= "('Saturn',   568,    10.7,   -140,   1),"  & @CR
$sSQL2 &= "('Uranus',   86.8,   17.2,   -195,   1),"  & @CR
$sSQL2 &= "('Neptune',  102,    16.1,   -200,   1),"  & @CR
$sSQL2 &= "('Pluto',    0.0131, 153.3,  -225,   0)"  & @CR
;--------------------------------------------------------------------------------------------------

Say('Deleting previous ' & $sDB_SolarSystem & ' database: ' & _SQLiteEx_DropDatabase($sDB_SolarSystem))

Say('Opening database ' & $sDB_SolarSystem & ': ' & _SQLiteEx_Open($sDB_SolarSystem, $sSQL2))

_SQLiteEx_SetTable($sTablePlanets)

$aRow = _SQLiteEx_QuerySingleRow('SELECT AVG(length_of_day) FROM ' & $sTablePlanets)
Say('Result of queue - Average length of the day in our solar system: ' & $aRow[0] & ' hours')

$aFirstEntry = _SQLiteEx_FirstTableEntry()
Say('First table entry (planet): ' & $aFirstEntry[0])

$aLastEntry = _SQLiteEx_LastTableEntry()
Say('Last table entry (planet): ' & $aLastEntry[0])

_SQLiteEx_ShowTable()

Say('Is first database ' & $sDB_Earth & ' still exists? ' & _SQLiteEx_DatabaseExists($sDB_Earth))

Say('Dropping database ' & $sDB_Earth & ': ' & _SQLiteEx_DropDatabase($sDB_Earth))

_SQLiteEx_Close()


Func Say($sMsg)
    If $bVerbose Then
        MsgBox(0, 'SQLiteEx Example Says', $sMsg)
    Else
        _FileWriteLog('SQLitex_Example.log', $sMsg)
    EndIf
EndFunc

SQLiteEx.au3

SQLiteEx_Examples.au3

Edited by 57ar7up
Link to comment
Share on other sites

Interesting. 
I finally find the time to SQLite. 
You gave me motivation.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

  • 1 year later...

I am sorry for maybe asking a very elemental question, but I am trying to use your excellent UDF to display a table from a sqlite db file within an existing gui.  I tried just using the _SQLiteEx_ShowTable to pop up a separate GUI, but it would destroy my original gui, which is not the goal.  If it's possible to click a button, like Refresh and display the simple array inside the existing GUI, that would be optimal.  Could you direct me towards an answer, please?  Thank you so much!

Link to comment
Share on other sites

@dimmyr
 
Here is function _SQLiteEx_GetTable() which is based on original _SQLiteEx_ShowTable()
but this one returns array (and don't display it) so you can do with returned array what you want ...

Func _SQLiteEx_GetTable($sTBL = Default, $sColumn = 'RowID', $sSortType = 'ASC') ;Displays current table
    If $sTBL = Default Then
        If $_sTable = FALSE Then
            Return SetError(1)
        Else
            $sTable = $_sTable
        EndIf
    Else
        $sTable = $sTBL
    EndIf
    If $_sTable = FALSE Then Return SetError(2)
    Local $aResult, $iRows, $iColumns, $iRval
    $sSQL = 'SELECT * FROM ' & $_sTable & ' ORDER BY ' & $sColumn & ' ' & $sSortType
    $iRval = _SQLite_GetTable2d(-1, $sSQL, $aResult, $iRows, $iColumns)
    If $iRval = $SQLite_OK Then
        Return $aResult
    Else
        Return SetError(3)
    EndIf
EndFunc

EDIT:
Also I don't beleive that original function SQLiteEx_ShowTable() would destroy your GUI because it just call _ArrayDisplay(). So you may post your code snipet (small reproducing script) ...

Edited by Zedna
Link to comment
Share on other sites

Just a note to present and future unsuspecting users of this UDF: there is no such thing as the "first" and the "last" table entries. SQL tables are orderless and automatic rowids are not a sequence index. Those functions carry misleading names and offer no guaranty of any kind regarding a row being "first" or "last" from an application point of view.

Easy short example:

drop table orderless;
create table Orderless (MyValue int, CONSTRAINT "" PRIMARY KEY (MyValue));
insert into Orderless values (1), (2), (3), (4), (5);
select rowid, * from orderless;

/*
rowid   MyValue
1     1
2     2
3     3
4     4
5     5
*/

insert or replace into orderless values (1);
select rowid, * from orderless;

/*
rowid   MyValue
2     2
3     3
4     4
5     5
6     1
*/

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)

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