Jump to content
Seminko

[SOLVED] --> [SQLite] Load extension

Recommended Posts

Seminko
Posted (edited)

In my recent project I'm downloading a bunch of data, so I decided to store it in a SQLite database. NOTE: I'm using sqlite3_x64.dll

Everything is working just fine but I'm struggling with getting the Median value. SQLite has an Average function but not a Median one.

I googled but all of the provided solutions are way above my pay-grade. After some more searching I found 'extension-functions.c' on the SQLite site where Median is included. After almost an hour of struggling I was able to successfully compile it into a DLL.

So I downloaded @jchd's SQLiteExtLoad.au3 as seen here:

But I'm getting these errors:

"Path\SQLiteExtLoad.au3"(21,40) : warning: $g_hDll_SQLite: possibly used before declaration.
    Local $RetVal = DllCall($g_hDll_SQLite,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
"Path\SQLiteExtLoad.au3"(21,40) : error: $g_hDll_SQLite: undeclared global variable.
    Local $RetVal = DllCall($g_hDll_SQLite,
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

So I tried, copying the function to my file and changing the DLL variable ($g_hDll_SQLite) directly to the DLL location (C:\...\.. .dll), but now this error fires

If __SQLite_hChk($hConn, 1) Then Return SetError(@error, 0, $SQLITE_MISUSE)

To be honest, I don't know what to use as the $hConn - handle of connection.

I would appreciate any help, be it getting the median using SQLite queries or getting the DLL extension loaded using AutoIt.

Thanks, S.

 

EDIT: well, I suspect the $hConn variable refers to the return value of the _SQLite_Open function. Well, at least now _SQLite_EnableExtensions doesn't give errors. Now I run into problems with _SQLite_LoadExtension, which gives error -1, and extended 1. Apparently the 1 constant is a generic error where other error do not apply.

BTW, anybody knows whether I need to compile the extension DLL "into" x64 when I use a x64 SQLite? That might be the problem...

 

EDIT2: I recompiled the dll and tried it using the SQLite3.exe and it works, so I'm confident the extension DLL has been created correctly

median.JPG

Edited by Seminko

Share this post


Link to post
Share on other sites
user4157124

Possible workaround to return median value from a column in SQLite:

SELECT
    avg(name_column)
FROM (
    SELECT
        name_column
    FROM
        name_table
    ORDER BY
        name_column
    LIMIT
        2 - (
            SELECT
                count(*)
            FROM
                name_table
        ) % 2
    OFFSET (
            SELECT
                (count(*) - 1) / 2
            FROM
                name_table
    )
);

Share this post


Link to post
Share on other sites
Seminko
55 minutes ago, user4157124 said:

Possible workaround to return median value from a column in SQLite:

Thanks man. This works great when I need to grab median from single column.

However, I'm grouping this thing by like 7 columns.

CREATE TEMPORARY TABLE avgprices AS

    SELECT Znacka, Model, Verze, Inzerat_ID, Rok_vyroby, Najeto, Najeto_kategorie, Obsah, Obsah_updated, Palivo, Link, Popis, Cena, round(avg(Cena), 0) as Averageif, Count(*) as PocetInz
    
    FROM TableName
    
    GROUP BY Znacka, Model, Verze, Rok_vyroby, Najeto_kategorie, Obsah_updated, Palivo

Not really sure how to incorporate your code...

Despite us deviating from AutoIt, I still very much appreciate the support! Hopefully the mods won't mind that much ;)

Share this post


Link to post
Share on other sites
user4157124

No easy way in SQLite. Example (single GROUP BY):

DROP TABLE IF EXISTS table_temp;
CREATE TEMPORARY TABLE IF NOT EXISTS table_temp AS
    SELECT
        name_group,
        name_value
    FROM
        name_table
    ORDER BY
        name_group ASC,
        name_value ASC
;
SELECT
    t1.name_group,
    t1.name_value
FROM (
    SELECT
        rowid,
        name_group,
        name_value
    FROM
        table_temp
) AS t1
JOIN (
    SELECT
        name_group,
        CAST(((min(rowid) + max(rowid)) / 2) AS INTEGER) AS rowmid
    FROM
        table_temp AS t2
    GROUP BY
        name_group
) AS t3 ON t1.rowid = t3.rowmid
;


$hConn expects handle as returned from _SQLite_Open(). Example:

#include 
#include "SQLiteExtLoad.au3"

Global Const $g_sFileDb    = @ScriptDir & '\db.sqlite'
Global Const $g_sDllExt    = @ScriptDir & '\extname.dll'
Global Const $g_sDllSQLite = _SQLite_Startup()

Global       $g_hDb        = _SQLite_Open($g_sFileDb)

_SQLite_EnableExtensions($g_hDb, 1)
_SQLite_LoadExtension($g_hDb, $g_sDllExt)

;

_SQLite_Close($g_hDb)
_SQLite_Shutdown()

  • Like 1

Share this post


Link to post
Share on other sites
Seminko
26 minutes ago, user4157124 said:

$hConn expects handle as returned from _SQLite_Open(). Example:

#include 
#include "SQLiteExtLoad.au3"

Global Const $g_sFileDb    = @ScriptDir & '\db.sqlite'
Global Const $g_sDllExt    = @ScriptDir & '\extname.dll'
Global Const $g_sDllSQLite = _SQLite_Startup()

Global       $g_hDb        = _SQLite_Open($g_sFileDb)

_SQLite_EnableExtensions($g_hDb, 1)
_SQLite_LoadExtension($g_hDb, $g_sDllExt)

;

_SQLite_Close($g_hDb)
_SQLite_Shutdown()

 

I'm getting away from using nested selects because it is slow as hell. But thank you for the example script.

Please check my first post, I made some updates. I figured the $hConn was returned by _SQLite_Open(), but it still doesn't work for me.

Btw, have you tried the _SQLite_EnableExtensions function? It doesn't give you errors?

Share this post


Link to post
Share on other sites
Seminko
Posted (edited)

So, apparently, the partial issue was that $g_hDll_SQLite was renamed to $__g_hDll_SQLite, which I updated in JCHD's script.

Still receiving errors, though...

Error code: -1 (SQLite Reported an Error (Check @extended Value))

@extended: 1 (apparently that is a generic code)

 

My code:

Spoiler
#include <SQLite.au3>
#include <SQLite.dll.au3>
#include "SQLiteExtLoad.au3"

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

Global Const $g_sFileDb    = @ScriptDir & '\test.db'
Global Const $g_sDllExt    = @ScriptDir & '\median.dll'
Global Const $g_sDllSQLite = _SQLite_Startup()

Global       $g_hDb        = _SQLite_Open($g_sFileDb)

_SQLite_EnableExtensions($g_hDb, 1)
If @error Then
    MsgBox(1, "", "error1: " & @error & " / " & @extended)
EndIf

_SQLite_LoadExtension($g_hDb, $g_sDllExt)
If @error Then
    MsgBox(1, "", "error2: " & @error & " / " & @extended)
EndIf

$iRval = _SQLite_GetTable2d(-1, "SELECT median(Time) FROM SpeedTest", $aResult, $iRows, $iColumns)

If $iRval = $SQLITE_OK Then
    _ArrayDisplay($aResult)
Else
    MsgBox(1, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndIf

_SQLite_Close($g_hDb)
_SQLite_Shutdown()

 

 

JCHD's file updated:

Spoiler
;; SQLiteExtLoad.au3

#include-once

; #FUNCTION# ====================================================================================================================
; Name...........: _SQLite_EnableExtensions
; Description ...: Enables or disables loading of SQLite extensions
; Syntax.........: _SQLite_EnableExtensions($hConn, $Enable = 1)
; Parameters ....: $hConn       handle of connection
;                  $Enable      1 to enable (default) or 0 to disable
; Return values .: none
;                  @error Value(s):       -1 - SQLite Reported an Error (Check @extended Value)
;                  1 - Call prevented by safe mode (invalid handle)
;                  2 - Error calling SQLite API 'sqlite3_enable_load_extension'
;                  @extended Value(s): Can be compared against $SQLITE_* Constants
; Author ........: jchd
; ===============================================================================================================================

Func _SQLite_EnableExtensions($hConn, $Enable = 1)
    If __SQLite_hChk($hConn, 1) Then Return SetError(@error, 0, $SQLITE_MISUSE)
    Local $RetVal = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_enable_load_extension", "ptr", $hConn, "int", $Enable)
    If @error Then
        Return(SetError(2, 0, 0))
    Else
        If $RetVal[0] <> $SQLITE_OK Then Return(SetError(-1, $RetVal[0], 0))
    EndIf
EndFunc   ;==>__SQLite_EnableExtensions


; #FUNCTION# ====================================================================================================================
; Name...........: _SQLite_LoadExtension
; Description ...: Loads an SQLite extension for current connection
; Syntax.........: _SQLite_LoadExtension($hConn, $sFullPath [, $sEntry = ''])
; Parameters ....: $hConn       handle of the connection for which the extension will be loaded
;                  $sFullPath   path and name of the extension DLL
;                  $sEntry      name of entry point, defaults to 'sqlite3_extension_init'
; Return values .: none
;                  @error Value(s):       -1 - SQLite Reported an Error (Check @extended Value)
;                  1 - Call prevented by safe mode (invalid handle)
;                  2 - Error while converting path to UTF-8
;                  3 - Error calling SQLite API 'sqlite3_load_extension'
;                  @extended Value(s): Can be compared against $SQLITE_* Constants
; Author ........: jchd
; ===============================================================================================================================

Func _SQLite_LoadExtension($hConn, $sFullPath, $sEntry = 'sqlite3_extension_init')
    If __SQLite_hChk($hConn, 1) Then Return SetError(@error, 0, $SQLITE_MISUSE)
    Local $tDllPath = __SQLite_StringToUtf8Struct($sFullPath)
    If @error Then Return(SetError(2, @extended, 0))
    Local $RetVal = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_load_extension", _
                                                "ptr", $hConn, _
                                                "ptr", DllStructGetPtr($tDllPath), _
                                                "str", $sEntry, _
                                                "ptr", 0)
    If @error Then
        Return(SetError(3, 0, 0))
    Else
        If $RetVal[0] <> $SQLITE_OK Then Return(SetError(-1, $RetVal[0], 0))
    EndIf
EndFunc   ;==>_SQLiteLoadExtension


; #FUNCTION# ====================================================================================================================
; Name...........: _SQLite_LoadAutoExtension
; Description ...: Permanently loads an SQLite extension for current session
; Syntax.........: _SQLite_LoadAutoExtension($sFullPath [, $sEntry = ''])
; Parameters ....: $sDllPath    path of the extension DLL
;                  $sEntry      optional name of entry point, defaults to 'sqlite3_extension_init'
; Return values .: none
;                  @error Value(s):       -1 - SQLite Reported an Error (Check @extended Value)
;                  1 - Error while loading extension DLL
;                  2 - Error obtaining address of named entry point
;                  3 - Error calling SQLite API 'sqlite3_auto_extension'
;                  @extended Value(s): Can be compared against $SQLITE_* Constants
; Author ........: jchd
; ===============================================================================================================================

Func _SQLite_LoadAutoExtension($sFullPath, $sEntry = 'sqlite3_extension_init')
    Local $RetVal = DllCall("kernel32.dll", "ptr", "LoadLibraryW", "wstr", $sFullPath)
    If (@error Or $RetVal[0] = 0) Then Return(SetError(1, 0, 0))
    $RetVal = DllCall('kernel32.dll', 'ptr', 'GetProcAddress', 'ptr', $RetVal[0], 'str', $sEntry)
    If (@error Or $RetVal[0] = 0) Then Return(SetError(2, 0, 0))
    $RetVal = DllCall($__g_hDll_SQLite, "none:cdecl", "sqlite3_auto_extension", "ptr", $RetVal[0])
    If @error Then
        Return(SetError(3, 0, 0))
    Else
        If $RetVal[0] <> $SQLITE_OK Then Return(SetError(-1, $RetVal[0], 0))
    EndIf
EndFunc   ;==>_SQLite_LoadAutoExtension

 

 

Edited by Seminko

Share this post


Link to post
Share on other sites
Seminko
Posted (edited)

UPDATE:

The issue got solved. I incorrectly compiled the median.dll. It seems that despite me using MinGQ-W64, I somehow messed up the install and the compiled DLL ended up being 32bit (I'm using a 64bit SQLite DLL).

I re-installed MinGQ-W64, recompiled the DLL and it works now.

 

For all of you having issues with JCHD's SQLiteExtLoad.au3, replace '$g_hDll_SQLite' with '$__g_hDll_SQLite'.

 

Thanks for you help mr. @user4157124

Edited by Seminko

Share this post


Link to post
Share on other sites
jchd

You're correct. The posted script dates from ages and x64 wasn't popular at this time. Also x64 is often slower than x86 in many use cases.

The global naming convention changed in the meantime.

About subqueries: are you sure you have created convenient indices? Try prepending EXPLAIN QUERY PLAN to you query and look at the output to see if any new index can speed up things.

  • Like 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)

Share this post


Link to post
Share on other sites
Seminko
Posted (edited)
17 minutes ago, jchd said:

You're correct. The posted script dates from ages and x64 wasn't popular at this time. Also x64 is often slower than x86 in many use cases.

The global naming convention changed in the meantime.

About subqueries: are you sure you have created convenient indices? Try prepending EXPLAIN QUERY PLAN to you query and look at the output to see if any new index can speed up things.

Thanks for the reply.

Since the median extension works now, there is no need to dabble into the queries. It works great and is pretty fast :) .

Edited by Seminko

Share this post


Link to post
Share on other sites
jchd

Fine. This advice is nevertheless quite generic and helps to find optimization clues.


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)

Share this post


Link to post
Share on other sites
Seminko
2 minutes ago, jchd said:

Fine. This advice is nevertheless quite generic and helps to find optimization clues.

I will make sure to look into it!

Share this post


Link to post
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

  • Similar Content

    • Eminence
      By Eminence
      Hello,
      Using SQLite, I was trying to select a specific column with having duplicate entries removed by using the DISTINCT function. 
      Local $sQuery = "SELECT DISTINCT supervisor, COUNT(DISTINCT employee_name) FROM data_db What the above snippet does is just list only one supervisor but with the total count of unique employee names in the whole database when it should be listing out all supervisor entries from the database and list the total count of employees per each supervisors. If I remove the COUNT function, it does list out all unique entries of supervisor names from the list. Attached is a screenshot of an example database as well. 
      Any help will be much appreciated. Thank you!

    • Seminko
      By Seminko
      Hey,
      I have a script that uses SQLite. It worked without a hiccup on my W7 system.
      However, last week I bought a new machine, installed W10 and autoit, and now I'm getting an error during _SQLite_Startup.
      Can anyone advise on how to find what's wrong? Tried checking $__g_hPrintCallback_SQLite but that returns nothing.
       
      Tried downloading the latest version of the dll from the link in the function page and I also replaced the default SQLite.dll.au3 that came with AutoIt with the one that came with the latest SQLite version zip.
       
      Relevant parts of my script:
      #include <SQLite.au3> #include <SQLite.dll.au3> _SQLite_Startup(@ScriptDir & "\Config\sqlite3_x64.dll", False, 1) If @error Then MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded! - " & $__g_hPrintCallback_SQLite & @CRLF & @CRLF & "Exiting application / Zavolej Honzovi") Exit -1 EndIf  
      Thanks
      S.
    • FrancescoDiMuro
      By FrancescoDiMuro
      Good evening everyone
      I am building a management for the company I work with, and I just imported a real amount of rows ( about 29000 ), in my SQLite DB.
      The thing I am not understanding, is the time that the script takes to build this amount of rows in the ListView.
      I didn't measure it, but I think it took 2 minutes or so to create each ListView item...
      It is normal that it takes so much time?
      What can I do to improve the creation of the items?

      Here's the code I am using to query and to create ListView items...
      ; Articles ListView: Global $lvwArticles = GUICtrlCreateListView("ID|Fornitore|Codice|Descrizione|EU|Prezzo|Sconto Applicato|Note", 14, 87, 1507, 660, BitOR($GUI_SS_DEFAULT_LISTVIEW,$LVS_SORTASCENDING,$LVS_SORTDESCENDING), BitOR($WS_EX_CLIENTEDGE,$LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT)) ; Query $strQuery = "SELECT * FROM ARTICOLI;" ; Query Execution _SQLite_GetTable2d($objDatabase, $strQuery, $arrResult, $intRows, $intColumns) If @error Then ; Error Handling Else ; Cleaning the ListView _GUICtrlListView_DeleteAllItems($lvwArticles) If @error Then ; Error Handling Else ; No records in the Table If UBound($arrResult) < 2 Then ; Error Handling Else _GUICtrlListView_BeginUpdate($lvwArticles) For $intCounter = 1 To UBound($arrResult) - 1 $strListViewItem = $arrResult[$intCounter][0] & "|" & _ $arrResult[$intCounter][1] & "|" & _ $arrResult[$intCounter][2] & "|" & _ $arrResult[$intCounter][3] & "|" & _ $arrResult[$intCounter][4] & "|" & _ $arrResult[$intCounter][5] & "|" & _ $arrResult[$intCounter][6] & "|" & _ $arrResult[$intCounter][7] $objListViewItem = GUICtrlCreateListViewItem($strListViewItem, $lvwArticles) Next _GUICtrlListView_EndUpdate($lvwArticles) EndIf EndIf EndIf Thanks in advance


      Best Regards.
    • dangr82
      By dangr82
       
      I have created this function for a database, but I can not make it work. I always have two error messages: "not an error" ... and the file created, in the script directory, does not contain anything.
      Global $sDBName = "Hen.db" Func DatabaseTable() Local $sConnDB _SQLite_Startup() If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite.dll Can't be Loaded!") Exit -1 EndIf $sConnDB = _SQLite_Open($sDBName) If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Can't Load Database!") Exit -1 EndIf If Not _SQLite_Exec($sDBName, 'CREATE TABLE Animal ("Name", "Age");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) If Not _SQLite_Exec($sDBName, 'INSERT INTO Animale VALUES ("Charlie","5");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) _SQLite_Close($sConnDB) _SQLite_Shutdown() EndFunc  
    • Eminence
      By Eminence
      Hello,
      I've been searching on how to select the first row in my database which includes a time column based on the latest start_time date for today's date.
      This is my current code:
      Local $iCurrentDate = _Now() Local $sqlHandle = _dbOpen($sDbPath) Local $aResult, $iRows, $iCols _SQLite_GetTable2d($sqlHandle, "SELECT start_time FROM " & $srawDb & " ORDER BY datetime(start_time) DESC LIMIT 1", $aResult, $iRows, $iCols) _ArrayDisplay($aResult) _dbClose($sDbpath) What it does is it gets the first row entry in the database however it does not select the latest start_time entry in the database, it always select the first row. I've tried changing DESC to ASC to see if that will do it but the issue still remains. Any thoughts on this? Thanks in advance.
       
      *EDIT
      The date format in the database is by MM/DD/YYYY HH:MM:SS.
×