Jump to content

[SOLVED] --> [SQLite] Load extension


Recommended Posts

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
Link to post
Share on other sites

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
    )
);

Link to post
Share on other sites
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 ;)

Link to post
Share on other sites

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()

Link to post
Share on other sites
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?

Link to post
Share on other sites

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
Link to post
Share on other sites

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
Link to post
Share on other sites

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.

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 post
Share on other sites
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
Link to post
Share on other sites

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)

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By t0nZ
      Code to read a Spiceworks Database and export text data files in INI format.
      Focused on exporting data about PC inventory and useful to migrate to another inventory system.
      Tested with a SQLite DB from Spiceworks latest (and LAST on premise) version - (7.5.00107.30 ott 2019)
      It creates one  text file for every single machine.
       
      ;Spiceworks Db Exporter ; ;- NSC - t0nZ 2021 ;code to read a Spiceworks Database and export text data files in INI format. ;focused on exporting data about PC inventory. ;useful to migrate to another inventory system. ;tested with a SQLite DB from Spiceworks latest (and LAST on premise) version - (7.5.00107.30 ott 2019) ;It creates one text files for every single machine. ; --> please adapt paths to your environment. #include <SQLite.au3> #include <String.au3> #include <File.au3> dataINIfromDBspiceworks() #Region spiceworks immport Func dataINIfromDBspiceworks() ConsoleWrite("start import from db Spiceworks" & @CRLF) Local $dbspicepath = "C:\scambio" Local $dbspice = "spiceworks_prod.db" ; =====================>>>>> START SQL DLL _SQLite_Startup() ;======================<<<<<<<<<<<<<<<<<<< _SQLite_Open($dbspicepath & '\' & $dbspice) ;- Local $aCPdata Local $hQuery Local $recordcount = 0 Local $salvarec = 0 ;------------------------------------------------ Local $spiceQuery = "SELECT serial_number," & _ "server_name," & _ "manufacturer," & _ "(model || '-' || raw_model)," & _ "processor_type," & _ "raw_processor_type," & _ "processor_architecture," & _ "raw_processor_type," & _ "number_of_processors," & _ "'speed'," & _ "CAST (memory AS FLOAT) / 1073741824," & _ "current_user," & _ "domain," & _ "network_adapters.dns_domain," & _ "'logon'," & _ "operating_system," & _ "os_architecture," & _ "version," & _ "'lang'," & _ "('C:;Fixed;' || (CAST (disks.size AS FLOAT) / 1048576) || ';' || (CAST (disks.free_space AS FLOAT) / 1048576) )," & _ "devices.ip_address," & _ "'(0.0.0.0)'," & _ "'(0.0.0.0)'," & _ "'(0.0.0.0)'," & _ "'(0.0.0.0)'," & _ "network_adapters.gateway," & _ "(network_adapters.description ||' - '|| network_adapters.name)," & _ "devices.mac_address," & _ "devices.updated_on," & _ "user_tag " & _ "FROM devices " & _ "inner JOIN " & _ "network_adapters ON network_adapters.computer_id = devices.id " & _ "inner JOIN " & _ "disks ON disks.computer_id = devices.id " & _ "WHERE disks.name = 'C:' " & _ "ORDER BY devices.updated_on DESC;" _SQLite_Query(-1, $spiceQuery, $hQuery) While _SQLite_FetchData($hQuery, $aCPdata) = $SQLITE_OK writeINI($aCPdata) $recordcount += 1 If $recordcount = $salvarec + 10 Then $salvarec = $recordcount ConsoleWrite($recordcount & " processed records " & @CRLF) EndIf WEnd ;________________________________________________ _SQLite_Close() _SQLite_Shutdown() EndFunc ;==>dataINIfromDBspiceworks Func writeINI($aCPdata) Local $folderdataINI = "c:\scambio\ini" If Not FileExists($folderdataINI) Then DirCreate($folderdataINI) $aCPdata[28] = StringRegExpReplace($aCPdata[28], "[\D]", "") ; this "2021-04-17 02:34:16" to that "20210417023416 Local $cpini = $folderdataINI & "\" & "cp_" & $aCPdata[28] & "_" & $aCPdata[0] & "_.ini" If FileExists($folderdataINI & "\" & "cp_" & $aCPdata[28] & "_" & $aCPdata[0] & "_.ini") Then FileDelete($folderdataINI & "\" & "cp_" & $aCPdata[28] & "_" & $aCPdata[0] & "_.ini") ; deleted previous files ! ConsoleWrite("deleted: " & "cp_" & $aCPdata[28] & "_" & $aCPdata[0] & "_.ini" & @CRLF) EndIf ;Section Unique :serial, computer name Local $aSectionUnique[2][2] = [["serial", $aCPdata[0]], ["computername", $aCPdata[1]]] IniWriteSection($cpini, "UNIQUE", $aSectionUnique, 0) ;Section Machine :manufacturer,model,cpuname,cpuid,cpuarc,cpuvendor,cpucores,cpuspeed,RAM,disk1capacity,disk1used Local $aSectionMachine[9][2] = [["manufacturer", $aCPdata[2]], ["model", $aCPdata[3]], ["cpuname", $aCPdata[4]], ["cpuid", $aCPdata[5]], ["cpuarc", $aCPdata[6]], ["cpuvendor", $aCPdata[7]], ["cpucores", $aCPdata[8]], ["cpuspeed", $aCPdata[9]], ["ram", $aCPdata[10]]] IniWriteSection($cpini, "MACHINE", $aSectionMachine, 0) ;Section User : username,domain,DNSdomain,logonServer Local $aSectionUser[4][2] = [["username", $aCPdata[11]], ["domain", $aCPdata[12]], ["DNSdomain", $aCPdata[13]], ["logonServer", $aCPdata[14]]] IniWriteSection($cpini, "USER", $aSectionUser, 0) ;Section OS : OSver, OSarch, OSbuild, OSlang Local $aSectionOS[4][2] = [["OSver", $aCPdata[15]], ["OSarch", $aCPdata[16]], ["OSbuild", $aCPdata[17]], ["OSlang", $aCPdata[18]]] IniWriteSection($cpini, "OS", $aSectionOS, 0) ;Section Disks: username,domain,DNSdomain,logonServer Local $aSectionDrives[1][2] = [["drives", $aCPdata[19]]] IniWriteSection($cpini, "DRIVES", $aSectionDrives, 0) ;Section network:localIP1,localIP2,localIP3,localIP4,publicIP,GW,adapter,mac Local $aSectionNetwork[8][2] = [["localIP1", $aCPdata[20]], ["localIP2", $aCPdata[21]], ["localIP3", $aCPdata[22]], ["localIP4", $aCPdata[23]], ["publicIP", $aCPdata[24]], ["GW", $aCPdata[25]], ["adapter", $aCPdata[26]], ["mac", $aCPdata[27]]] IniWriteSection($cpini, "NETWORK", $aSectionNetwork, 0) ;Section PLUS :date, groupid Local $aSectionPlus[2][2] = [["date", $aCPdata[28]], ["groupid", $aCPdata[29]]] IniWriteSection($cpini, "PLUS", $aSectionPlus, 0) EndFunc ;==>writeINI #EndRegion spiceworks import  
      Spiceworks on premise, now pretty abadonware, has a non crypted SQLite DB usually located in:
      c:\Program Files (x86)\Spiceworks\db\spiceworks_prod.db The query was the difficult part (at least for me), and I export in INI format because it was part of my effort to migrate from Spiceworks to a custom made system (Computer Plucker see this post) where I already parse .INI files in a custom MySQL and/or SQLite DB.
       
    • By benners
      I am trying to normalise a database to remove duplicate info. I am using SQLite Expert to design the database and test sql queries.
      The database is going to store information about setup installers, such as paths, installer specific info, users to install for, type and category of installer blah, blah.
      I have attached the database thus far and the tables function are as follows:-
      category - stores text describing the general usage the installer comes under, such as Browser, Compression etc. installer - this is the main table that has relationships with the other tables and stores info about the installer file, install order etc. installer_user - a link table. Stores the user or computer to install the program for or on. package - stores the type of installer, NSIS, Inno  Nullsoft etc. platform - the OS architecture the installer file is compiled for. postinstall - a list of activities to perform when the main install has finished. postinstall_user - a link table. Stores the users\computers that are allowed to run the post install actions user - a list of computers or usernames. I might separate into two tables, undecided yet. Now there wil be one program that deals with the installation side and another that acts as a front end for editing the database suchs as adding new files, removing old files etc.
      The idea with the editing side is to be able to delete an installer from the installer table say with the id of 1 and all other pertinent information in the other tables will also be deleted. The same goes for deleting a user. All the fields relating to that user will be removed.
      I have managed to get that part working for the most part. If I delete either a user or installer, the related info in the installer_user and postinstall tables are removed but since I added the postinstall_user table to link usernames to the postinstall action, this is where I get the foreign key error. If someone can explain why, I am sure it is an obvious reason for someone who knows what they are doing 😄
      Cheers
      Installer - Copy.db
    • By dmob
      So I am trying to implement an archive system of sorts for my (SQLite) DB app. I wrote a function to attach a separate (archive) DB and
      sync the columns with main DB. If archive DB file does not exist, create file with _SQLiteOpen then close the file (and thus connection) with SQLite_Close.
      This works as intended, however, after the create operation, all subsequent _SQLite_* functions returned a "Library misuse error".
      After a little digging I found the problem in the _SQLite_Close function: it clears the "last opened database" handle even when there still is a live
      DB connection open. All other functions then "think" there is no DB connection active. I hacked two functions in the UDF for a quick fix:

      In _SQLite_Close: Change
      ... $__g_hDB_SQLite = 0 __SQLite_hDel($__g_ahDBs_SQLite, $hDB) Return $iRval[0] to:
      $__g_hDB_SQLite = __SQLite_hDel($__g_ahDBs_SQLite, $hDB) Return $iRval[0] and in Func __SQLite_hDel changed
      Func __SQLite_hDel(ByRef $ahLists, $hGeneric) Local $iElement = _ArraySearch($ahLists, $hGeneric) If $iElement > 0 Then _ArrayDelete($ahLists, $iElement) EndFunc ;==>__SQLite_hDel to:
      Func __SQLite_hDel(ByRef $ahLists, $hGeneric) Local $iElement = _ArraySearch($ahLists, $hGeneric) If $iElement > 0 Then _ArrayDelete($ahLists, $iElement) Return $ahLists[UBound($ahLists)-1] ; Return last opened db EndIf Return 0 EndFunc ;==>__SQLite_hDel so it preserves last opened DB again.
       
      My archive function now works great
      I'm not sure if this should be classified as a bug, but I believe so...
      Hope this helps someone before
    • By argumentum
      #include <SQLite.au3> ;-- When SQLite is compiled with the JSON1 extensions it provides builtin tools ;-- for manipulating JSON data stored in the database. ;-- This is a gist showing SQLite return query as a JSON object. ;-- https://www.sqlite.org/json1.html Example() Func Example() _SQLite_Startup() ; "<your path>\sqlite3.dll", False, 1) ; https://www.autoitscript.com/autoit3/docs/libfunctions/_SQLite_Startup.htm _SQLite_Open() ; ...if you can not run this due to errors, get the latest DLL from https://www.sqlite.org/ If _SQLite_Exec(-1, "CREATE TABLE users (id INTEGER PRIMARY KEY NOT NULL, full_name TEXT NOT NULL, email TEXT NOT NULL, created DATE NOT NULL );") Then Return 4 If _SQLite_Exec(-1, 'INSERT INTO users VALUES ' & _ '(1, "Bob McFett", "bmcfett@hunters.com", "32-01-01"),' & _ '(2, "Angus O''Vader","angus.o@destroyers.com", "02-03-04"),' & _ '(3, "Imperator Colin", "c@c.c", "01-01-01");') Then Return 5 ; -- Get query data as a JSON object using the ; -- json_group_object() [1] and json_object() [2] functions. _SQLite_GetTable2d_ArrayToConsole("SELECT" & _ " json_group_object(" & _ " email," & _ " json_object('full_name', full_name, 'created', created)" & _ " ) AS json_result" & _ " FROM (SELECT * FROM users WHERE created > ""02-01-01"");") ; {"bmcfett@hunters.com":{"full_name":"Bob McFett","created":"32-01-01"},"angus.o@destroyers.com":{"full_name":"Angus O'Vader","created":"02-03-04"}} ; -- Get query data as a JSON object using the ; -- json_group_array() function to maintain order. _SQLite_GetTable2d_ArrayToConsole("SELECT" & _ " json_group_array(" & _ " json_object('full_name', full_name, 'created', created)" & _ " ) AS my_json_result_OrAnythingReally" & _ " FROM (SELECT * FROM users ORDER BY created);") ; [{"full_name":"Imperator Colin","created":"01-01-01"},{"full_name":"Angus O'Vader","created":"02-03-04"},{"full_name":"Bob McFett","created":"32-01-01"}] ;-- Links ;-- [1] https://www.sqlite.org/json1.html#jgroupobject ;-- [2] https://www.sqlite.org/json1.html#jobj ; example found at https://gist.github.com/akehrer/481a38477dd0518ec0086ac66e38e0e2 EndFunc ;==>Example Func _SQLite_GetTable2d_ArrayToConsole($sSQL, $hDB = -1) Local $aResult, $iRows, $iColumns If _SQLite_GetTable2d($hDB, $sSQL, $aResult, $iRows, $iColumns) Then ConsoleWrite("! SQLite Error: " & _SQLite_ErrCode($hDB) & @CRLF & "! " & _SQLite_ErrMsg($hDB) & @CRLF) Else _SQLite_Display2DResult($aResult) EndIf ConsoleWrite(@CRLF) EndFunc ;==>_SQLite_GetTable2d_ArrayToConsole Based on this example, you can build your own query. 
      The code has all the explanations.
      Enjoy  
    • By argumentum
      #include <SQLite.au3> ;~ #include <SQLite.dll.au3> Local $hQuery, $aRow, $aNames _SQLite_Startup() ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF) _SQLite_Open() ; open :memory: Database _SQLite_Exec(-1, "CREATE TABLE aTest (A,B int not null unique ,C text);") _SQLite_Exec(-1, "INSERT INTO aTest(a,b,c) VALUES ('c','2','World');") _SQLite_Exec(-1, "INSERT INTO aTest(a,b,c) VALUES ('b','3',' ');") _SQLite_Exec(-1, "INSERT INTO aTest(a,b,c) VALUES ('a','1','Hello');") _SQLite_Query(-1, "SELECT _ROWID_,* FROM aTest ORDER BY a;", $hQuery) _SQLite_FetchTypes($hQuery, $aNames) ; Read out Column Types ConsoleWrite(StringFormat(" %-10s %-10s %-10s %-10s ", $aNames[0], $aNames[1], $aNames[2], $aNames[3]) & @CRLF) _SQLite_FetchNames($hQuery, $aNames) ; Read out Column Names ConsoleWrite(StringFormat(" %-10s %-10s %-10s %-10s ", $aNames[0], $aNames[1], $aNames[2], $aNames[3]) & @CRLF) While _SQLite_FetchData($hQuery, $aRow) = $SQLITE_OK ConsoleWrite(StringFormat(" %-10s %-10s %-10s %-10s ", $aRow[0], $aRow[1], $aRow[2], $aRow[3]) & @CRLF) WEnd _SQLite_Exec(-1, "DROP TABLE aTest;") _SQLite_Close() _SQLite_Shutdown() ; Output: ; INTEGER int text ; rowid A B C ; 3 a 1 Hello ; 2 b 3 ; 1 c 2 World Func _SQLite_FetchTypes($hQuery, ByRef $aTypes) Dim $aTypes[1] If __SQLite_hChk($hQuery, 3, False) Then Return SetError(@error, 0, $SQLITE_MISUSE) Local $avDataCnt = DllCall($__g_hDll_SQLite, "int:cdecl", "sqlite3_column_count", "ptr", $hQuery) If @error Then Return SetError(1, @error, $SQLITE_MISUSE) ; DllCall error If $avDataCnt[0] <= 0 Then Return SetError(-1, 0, $SQLITE_DONE) ReDim $aTypes[$avDataCnt[0]] Local $avColName For $iCnt = 0 To $avDataCnt[0] - 1 $avColName = DllCall($__g_hDll_SQLite, "wstr:cdecl", "sqlite3_column_decltype16", "ptr", $hQuery, "int", $iCnt) If @error Then Return SetError(2, @error, $SQLITE_MISUSE) ; DllCall error $aTypes[$iCnt] = $avColName[0] Next Return $SQLITE_OK EndFunc ;==>_SQLite_FetchTypes  If you wanna build a proper JSON string, you may want to know if is {"int":123} or {"text":"123"}
      and for that, this can help, obviously only when declared in the SQLite table.
×
×
  • Create New...