mattn / go-sqlite3

sqlite3 driver for go using database/sql
http://mattn.github.io/go-sqlite3
MIT License
8.05k stars 1.11k forks source link

How does SQLite accommodate various data types? #1275

Open imranimtiaz opened 2 months ago

imranimtiaz commented 2 months ago

It seems that with the current state of the go-sqlite3 driver, there's no built-in way to retrieve the data type of each result row dynamically. This becomes an issue when the actual data in a column doesn't match the data type defined by the table schema. For instance, if a column is declared as an integer but contains a string in some rows, attempting to retrieve that row can cause errors.

The problem seems to be related to the use of the sqlite3_column_decltype() function in the DeclTypes() method, which only returns the type declared at the schema level, rather than the actual data type of each individual row.

I've found that gwenn's SQLite driver for Go, while not as widely used anymore (since they’ve shifted focus to Rust), does offer a feature that lets you retrieve the actual type for each row of a result set. You can check it out in the code linked here: gosqlite stmt.go L550-L555.

Given go-sqlite3’s broader usage, I think it would be worth considering adding a similar feature to dynamically retrieve row-level data types in your driver. It would be particularly useful for scenarios where user-generated SQLite databases are involved, such as this one: DB4S download stats.

I also opened an issue a while back with more context on this in gwenn's repository (issue gwenn/gosqlite#5), in case that’s helpful.

What are your thoughts on adding this functionality? 😄

rittneje commented 2 months ago

I'm not entirely sure what you are intending to do, but have you tried just scanning into values of type any?