gosqlite
gosqlite copied to clipboard
Add some example code?
Going to try out the SQLite3 driver in this repo, as mattn's one doesn't seem to cope with SQLite's dynamic type system.
But... no example code for beginners to look at. ☹️
The reference info on godoc.org seems useful, but targeted to a more experienced audience.
Any chance of adding example code to this repo? Hopefully something that shows off the things useful here which aren't present in the commonly used driver (mattn's). :smile:
Hello, I suggest you use the mattn's driver because it is widely used, SQLite C library is bundled and mattn is working on this. I apologise for the lack of documentation. And in case you don't already know, it is also the database/sql/driver API that doesn't cope with SQLite's dynamic type system (for example, here).
Ahhh, I wasn't able to get things working with mattn's driver, but I did manage to make it work with yours.
You've moved focus from Go to Rust?
Ok, I see that you are using the low-level API (not the standard database/sql API). And do the same for Postgresql with pgx! You are trying to retrieve some data from a table without knowing its content, no ? Have you tried using the standard Scan method instead ?
If an argument has type *interface{}, Scan copies the value provided by the underlying driver without conversion.
And yes, for me, Rust is more rewarding than Go.
Yeah, in this particular application (similar to a web based database viewer) the database(s) are user supplied. Example screenshot to give the idea:
When I first tried the standard Scan method - using mattn's driver through database/sql - I just kept getting conversion errors, even using *interface{}.
There didn't seem to be a way to determine the correct concrete type to use for the cells in a result set. Looking at the mattn's driver docs on godoc.org, there's a DeclTypes() function which seems like it should serve that purpose, which I didn't try last time.
Looking at DeclTypes()
in mattn's driver, it uses "sqlite3_column_decltype", which doesn't seem fit for this purpose. The docs for that say it only returns the column type defined in the table schema.
With ColumnType()
in your driver, that uses sqlite3_column_type
which seems to do things correctly. At least, it's working here with database containing mixed data types in columns. (eg binary data in fields declared as text)
I guess for now I'll need to keep using your driver, and/or look at switching if something turns out to break further on which I can't fix. :smile:
If I'm getting this wrong somehow, I'm definitely open to doing things differently. Still very early in my Golang learning.
I suggest that:
- you use database/sql,
I think you can already/temporary do it with my driver.
You may need to deactivate the
ScanNumericalAsTime
flag with a customRegister
. - you ask mattn to make possible to disable the
DeclTypes()
usage inNext
. It seems feasible with aDynamicType
flag at the connection level and a customRegister
.
I once tried to describe the problem related to SQLite dynamic type and propose a solution here.
@gwenn Thanks. Yeah, getting this fixed in mattn's driver is on our list. Just not right now though. :wink: