Hello,
as someone may know, there is a SQLite.au3 (AutoIt3) script, supporting direct use of SQLite3.DLL and SQLite3.exe.
So I tried to translate it. Had to read a lot, think a lot and try a lot to understand the DLLCalls. But now there is a first result:
Code:
/*
;===============================================================================
; Include Version: 0.05 (2006/09/12)
;===============================================================================
;
; AHK Version: 1.0.44.08
; Language: English
; Description: Functions that assist access to an SQLite database.
;
;===============================================================================
; This software is provided 'as-is', without any express or
; implied warranty. In no event will the authors be held liable for any
; damages arising from the use of this software.
;===============================================================================
; Function list
;===============================================================================
_SQLite_Startup
_SQLite_Shutdown
_SQLite_OpenDB
_SQLite_CloseDB
_SQLite_GetTable
_SQLite_Exec
_SQLite_LibVersion
_SQLite_LastInsertRowID
_SQLite_GetTable2d
_SQLite_Changes
_SQLite_TotalChanges
_SQLite_ErrCode
_SQLite_ErrMsg
_SQLite_Display2DResult
_SQLite_FetchData
_SQLite_Query
_SQLite_SetTimeout
_SQLite_SaveMode
_SQLite_QueryFinalize
_SQLite_QueryReset
_SQLite_FetchNames
_SQLite_QuerySingleRow
_SQLite_SQLiteExe
_SQLite_Encode
_SQLite_ExtractInt
;===============================================================================
; User Calltips:
;===============================================================================
_SQLite_Startup() Loads SQLite3.dll
_SQLite_Shutdown() Unloads SQLite3.dll
_SQLite_Open([$sDatabase_Filename]) Opens Database, Sets Standard Handle, Returns Handle
_SQLite_Close([$hDB]) Closes Database
_SQLite_GetTable($hDB | -1 , $sSQL , ByRef $sResult , ByRef $iRows , ByRef $iColumns , [$iCharSize = 64]) Executes $sSQL Query to $sResult, Returns Error Code
_SQLite_Exec($hDB | -1 , $sSQL) Executes $sSQL (No Result), Returns Error Code
_SQLite_LibVersion() Returns Dll's Version No.
_SQLite_LastInsertRowID($hDB) Returns Last INSERT ROWID
_SQLite_GetTable2d($hDB | -1 , $sSQL , ByRef $aResult , ByRef $iRows , ByRef $iColumns , [$iCharSize = 64], [$fSwichDimensions = False]) Executes $sSQL Query to $aResult, Returns Error Code
_SQLite_Changes([$hDB]) Returns Number of Changes (Excluding Triggers) of The last Transaction
_SQLite_TotalChanges([$hDB]) Returns Number of All Changes (Including Triggers) of all Transactions
_SQLite_ErrCode([$hDB]) Returns Last Error Code (Numeric)
_SQLite_ErrMsg([$hDB]) Returns Last Error Message
_SQLite_Display2DResult($aResult , [$iCellWidth = 0], [$fReturn = False]) Returns or Prints a 2d Array to console
_SQLite_FetchData($hQuery, ByRef $aRow, [$fBinary = False] ) Fetches Results From First/Next Row of $hQuery Query into $aRow, Returns Error Code
_SQLite_Query($hDB | -1 , $sSQL , ByRef $hQuery) Prepares $sSql, Returns Error Code
_SQLite_SetTimeout([$hDB = -1] , [$iTimeout = 1000]) Sets Timeout for busy handler
_SQLite_SaveMode($fSaveModeState) Turn Savemode On or Off (boolean)
_SQLite_QueryFinalize($hQuery) Finalizes a Query
_SQLite_QueryReset($hQuery) Resets a Query
_SQLite_FetchNames($hQuery, ByRef $aNames) Read out the Tablenames of a _SQLite_Query() based query
_SQLite_QuerySingleRow($hDB | -1 , $sSQL , ByRef $aRow) Read out the first Row of the Result from the Specified query
_SQLite_SQLiteExe( $sDatabaseFile , $sInput , ByRef $sOutput , $sSQLiteExeFilename = "SQLite3.exe" ) Executes commands in SQLite.exe
_SQLite_Encode($vData) Returns Encoded String
;===============================================================================
; Changelog:
;===============================================================================
*/
#NoEnv
#SingleInstance force
SetBatchLines -1
SetWinDelay -1
;===============================================================================
; SQLite Returncodes
;===============================================================================
$SQLITE_OK := 0 ; /* Successful result */
$SQLITE_ERROR := 1 ; /* SQL error or missing database */
$SQLITE_INTERNAL := 2 ; /* An internal logic error in SQLite */
$SQLITE_PERM := 3 ; /* Access permission denied */
$SQLITE_ABORT := 4 ; /* Callback routine requested an abort */
$SQLITE_BUSY := 5 ; /* The database file is locked */
$SQLITE_LOCKED := 6 ; /* A table in the database is locked */
$SQLITE_NOMEM := 7 ; /* A malloc() failed */
$SQLITE_READONLY := 8 ; /* Attempt to write a readonly database */
$SQLITE_INTERRUPT := 9 ; /* Operation terminated by sqlite_interrupt() */
$SQLITE_IOERR := 10 ; /* Some kind of disk I/O error occurred */
$SQLITE_CORRUPT := 11 ; /* The database disk image is malformed */
$SQLITE_NOTFOUND := 12 ; /* (Internal Only) Table or record not found */
$SQLITE_FULL := 13 ; /* Insertion failed because database is full */
$SQLITE_CANTOPEN := 14 ; /* Unable to open the database file */
$SQLITE_PROTOCOL := 15 ; /* Database lock protocol error */
$SQLITE_EMPTY := 16 ; /* (Internal Only) Database table is empty */
$SQLITE_SCHEMA := 17 ; /* The database schema changed */
$SQLITE_TOOBIG := 18 ; /* Too much data for one row of a table */
$SQLITE_CONSTRAINT := 19 ; /* Abort due to constraint violation */
$SQLITE_MISMATCH := 20 ; /* Data type mismatch */
$SQLITE_MISUSE := 21 ; /* Library used incorrectly */
$SQLITE_NOLFS := 22 ; /* Uses OS features not supported on host */
$SQLITE_AUTH := 23 ; /* Authorization denied */
$SQLITE_ROW := 100 ; /* sqlite_step() has another row ready */
$SQLITE_DONE := 101 ; /* sqlite_step() has finished executing */
;===============================================================================
; SQLite Constants
;===============================================================================
$SQLITE_DBHANDLE := 1 ; /* (Internal Only) Database Handle (sqlite3*) */
$SQLITE_QUERYHANDLE := 2 ; /* (Internal Only) Query Handle (sqlite3_stmt*) */
;===============================================================================
; SQLite Globals
;===============================================================================
$g_sDll_SQLite := A_ScriptDir . "\SQLITE3.DLL"
$g_hDll_SQLite := 0
$g_sDB_SQLite := A_ScriptDir . "\Telefon.db"
$g_hDB_SQLite := 0
;===============================================================================
; Function Name: _SQLite_StartUP()
; Description: Loads SQLite3.dll
; Parameter(s): None
; Requirement: SQLite3.dll must exist in A_ScriptDir
; Return Value(s): On Success - $SQLITE_OK
; On Failure - ErrorLevel
; Author(s): nick
;===============================================================================
_SQLite_Startup()
{
Global
$g_hDll_SQLite := DllCall("LoadLibrary", "str", $g_sDll_SQLite)
If (ErrorLevel <> 0)
{
Return ErrorLevel
}
Return $SQLITE_OK
}
;===============================================================================
; Function Name: _SQLite_Shutdown()
; Description: Unloads SQLite3.dll
; Parameter(s): None
; Requirement: None
; Return Value(s): On Success - $SQLITE_OK
; On Failure - ErrorLevel
; Author(s): nick
;===============================================================================
_SQLite_Shutdown()
{
Global
DllCall("FreeLibrary", "UInt", $g_hDll_SQLite)
If (ErrorLevel <> 0)
{
Return ErrorLevel
}
$g_hDll_SQLite := 0
Return $SQLITE_OK
}
;===============================================================================
; Function Name: _SQLite_OpenDB()
; Description: Opens a Database
; Parameter(s): $sDatabase_Filename - Database Filename
; Optional - (uses ':memory:' db by default)
; Requirement: None
; Return Value(s): On Success - $SQLITE_OK
; On Failure - ErrorLevel or SQLite_RC
; Author(s): nick
;===============================================================================
_SQLite_OpenDB($sDatabase_Filename = "")
{
Local $i_RC
$i_RC := 0
If $sDatabase_Filename =
{
$sDatabase_Filename := ":memory:"
}
$i_RC := DllCall("sqlite3\sqlite3_open"
, "str", $sDatabase_Filename
, "Uint *", $g_hDB_SQLite
, "Cdecl int")
If (ErrorLevel <> 0)
{
Return Errorlevel
}
Return $i_RC
}
;===============================================================================
; Function Name: _SQLite_CloseDB()
; Description: Closes an open Database
; Waits until SQLite <> $SQLITE_BUSY until 'Timeout' has elapsed
; Parameter(s): $hDB - Optional Database Handle
; Requirement: None
; Return Value(s): On Success - Returns $SQLITE_OK
; On Failure - Errorlevel or SQLite_RC
; Author(s): nick
;===============================================================================
_SQLite_CloseDB($hDB = -1)
{
Local $i_RC
$i_RC := 0
$i_RC := DllCall("sqlite3\sqlite3_close"
, "Uint", $g_hDB_SQLite
, "Cdecl int")
If (ErrorLevel <> 0)
{
Return Errorlevel
}
$g_hDB_SQLite := 0
Return $i_RC
}
;===============================================================================
; Function Name: _SQLite_GetTable()
; Description: Passes Out String Containing Tablenames and Data of Executed Query
; Parameter(s): $hDB - An Open Database, Use -1 To use Last Opened Database
; $sSQL - SQL Statement to be executed
; ByRef $sResult - Passes out the Resultstring or SQLite ErrMsg
; ByRef $iRowCount - Passes out the amount of 'data' Rows
; ByRef $iColCount - Passes out the amount of Columns
; $iCharSize - Optional: specifies the maximal size of a Data Field
; Return Value(s): On Success - Returns $SQLITE_OK
; On Failure - ErrorLevel or SQLite_RC
; Author(s): nick
;===============================================================================
_SQLite_GetTable($hDB, $sSQL, ByRef $sResult, ByRef $iRowCount, ByRef $iColCount, $iCharSize = 64)
{
Local $i, $i_RC, $iErr, $iOff, $iRow, $iCol, $iResultSize, $pStrPtr, $pResult, $sCol, $sRow
$i := A_TickCount
$i_RC := 0
$pResult := 0
$iErr := 0
If ($iCharSize = 0 Or $iCharSize = "" Or $iCharSize < 1)
{
$iCharSize := 64
}
$i_RC := DllCall("sqlite3\sqlite3_get_table"
, "Uint", $g_hDB_SQLite
, "str", $sSQL
, "Uint *", $pResult
, "Uint *", $iRowCount
, "Uint *", $iColCount
, "Uint *", $iErr
, "Cdecl int")
If (ErrorLevel <> 0)
{
Return Errorlevel
}
If ($i_RC <> $SQLITE_OK)
{
$sResult := DllCall("sqlite3\sqlite3_errmsg", "Uint", $hDB, "Cdecl str")
Return $i_RC
}
$iResultSize := ($iRowCount + 1) * $iColCount
VarSetCapacity($sResult, $iCharSize * $iResultSize)
$iOff := 0
Loop % $iRowCount + 1
{
$sRow =
Loop %$iColCount%
{
$sCol =
DllCall("lstrcpyA", "Str", $sCol, "Uint", _SQLite_ExtractInt($pResult, $iOff))
$sRow = %$sRow%%$sCol%|
$iOff += 4
}
StringTrimRight $sRow, $sRow, 1
; MsgBox %$sRow%
$sResult = %$sResult%%$sRow%`n
}
VarSetCapacity($sResult, -1)
StringTrimRight $sResult, $sResult, 1
MsgBox % A_TickCount - $i
Return $i_RC
}
;===============================================================================
; Function Name: _SQLite_ExtractInt()
; Description: Extracts an Integer Value from the Address, $pResult points to
; Parameter(s): $pResult - Pointer
; $pOffset - Offset for Pointer
; $pIsSigned - Optional: Signed or Unsigned Integer (Default = False)
; $pSize - Optional: Size of Returnvalue (Default = 4)
; Return Value(s): Returns Integer Value of specified length
; Author(s): Chris Mallett & nick
;===============================================================================
_SQLite_ExtractInt(ByRef $pResult, $pOffset = 0, $pIsSigned = false, $pSize = 4)
{
Local $i_RC
$i_RC := 0
; Build the integer by adding up its bytes
Loop %$pSize%
{
$i_RC += *($pResult + $pOffset + A_Index-1) << 8*(A_Index-1)
}
If (!$pIsSigned OR $pSize > 4 OR result < 0x80000000)
{ ; Signed vs. unsigned doesn't matter in these cases
Return $i_RC
}
Else
{ ; Otherwise, convert the value (now known to be 32-bit) to its signed counterpart
Return -(0xFFFFFFFF - $i_RC + 1)
}
}
;===============================================================================
; Test Section
;===============================================================================
$iRows := 0
$iCols := 0
$sResult := ""
$sSQL := "SELECT * FROM Telefon Where Abteilung = 'P & O';"
$iRC := 0
$iRC := _SQLite_Startup()
If ($iRC <> 0)
{
MsgBox Fehler bei STARTUP: %$iRC%
}
$iRC := _SQLite_OpenDB($g_sDB_SQLite)
If ($iRC <> 0)
{
MsgBox Fehler bei OPENDB: %$iRC%
}
$iRC := _SQLite_GetTable($g_hDB_SQLite, $sSQL, $sResult, $iRows, $iCols)
If ($iRC <> 0)
{
MsgBox Fehler bei GETTABLE: %$iRC%
}
Msgbox Zeilen : %$iRows%, Spalten : %$iCols%
$iRC := _SQLite_CloseDB($g_hDB_SQLite)
If ($iRC <> 0)
{
MsgBox Fehler bei CLOSEDB: %$iRC%
}
$iRC := _SQLite_ShutDown()
If ($iRC <> 0)
{
MsgBox Fehler bei SHUTDOWN: %$iRC%
}
ExitApp
_SQLite_GetTable passes out the result of a QUERY to a string where column values are seperated by "|" and rows are seperated by LF.
It's even faster then AU3 (that's why I love AHK), but btw, can anybody explain, why the compiled version is about 30 % slower than the source version?
It was a hard job for an elder
HOST programmer so far, but if there is any need for other functions, I'll go on.
Have a lot of fun!