Jump to content

_SQLite_FetchTypes()


argumentum
 Share

Recommended Posts

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

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Don't believe this!

You can store any type in (almost) any column of any row, you can declare fantasy types or no type at all.

The only reliable way to know which default type a result column of the current row is is to invoke sqlite3_column_type(), which returns the type of the data as it is for each value of each row. Nonetheless you can still force conversion by calling a retrieval function for another type.

A column declared INT can store the string value 'abc123' in a given row; sqlite3_column_type() would return SQLITE_TEXT but you can call sqlite3_column_blob() to force conversion to binary and retrieve the corresponding blob.

Of course the conversions you can force this way don't always make sense. A picture stored as a 3.5 Mb blob value in a column declared CHAR and retrieved with sqlite3_column_int64() is going to be deceptive.

Edited by jchd

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

19 minutes ago, jchd said:

The only reliable way to know which default type a result column of the current row is is to invoke sqlite3_column_type(), which returns the type of the data as it is for each value of each row.

lol, yes, SQLite is some fantastic piece of code. The only column that'll adhere to INTEGER is the RowID. ( ...or PK if I understand that right )
But the expectation of a column defined as INT by the coder, is to pull INTegers from that column and attempt to use it as INT.
If I declare a column "MyStuffType", SQLite will return it as "MyStuffType" type in the above function.
Declarations do help organize the code, and if:

3 hours ago, argumentum said:

you wanna build a proper JSON string, you may want to know if is {"int":123} or {"text":"123"}

And that's the idea :) 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Under the assumption that correctly declared types are actually enforced by inserts and updates, then this function will work.

I only made the point to warn unsuspecting readers to fall into the trap.

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

×
×
  • Create New...