gosqlite icon indicating copy to clipboard operation
gosqlite copied to clipboard

Add some example code?

Open justinclift opened this issue 8 years ago • 8 comments

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:

justinclift avatar Nov 23 '16 20:11 justinclift

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

gwenn avatar Nov 24 '16 18:11 gwenn

Ahhh, I wasn't able to get things working with mattn's driver, but I did manage to make it work with yours.

justinclift avatar Nov 24 '16 19:11 justinclift

You've moved focus from Go to Rust?

justinclift avatar Nov 24 '16 19:11 justinclift

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.

gwenn avatar Nov 25 '16 19:11 gwenn

Yeah, in this particular application (similar to a web based database viewer) the database(s) are user supplied. Example screenshot to give the idea:

DBHub.io Database page

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.

justinclift avatar Nov 26 '16 01:11 justinclift

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.

justinclift avatar Nov 26 '16 02:11 justinclift

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 custom Register.
  • you ask mattn to make possible to disable the DeclTypes() usage in Next. It seems feasible with a DynamicType flag at the connection level and a custom Register.

I once tried to describe the problem related to SQLite dynamic type and propose a solution here.

gwenn avatar Nov 26 '16 09:11 gwenn

@gwenn Thanks. Yeah, getting this fixed in mattn's driver is on our list. Just not right now though. :wink:

justinclift avatar Jan 04 '17 16:01 justinclift