-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert date/datetimes to text when storing #160
Comments
I've tried to do this manually by defining
as is described for julia> SQLite.Query(db, "SELECT * FROM tmp") |> DataFrame
ERROR: TypeError: in typeassert, expected Union{Missing, String}, got DateTime
Stacktrace:
[1] sqlitevalue(::Type{Union{Missing, String}}, ::Ptr{Nothing}, ::Int64) at /home/<snip>/.julia/packages/SQLite/yKARA/src/SQLite.jl:220
[2] getvalue(::SQLite.Query{NamedTuple{(:id, :date),Tuple{Union{Missing, Int64},Union{Missing, String}}}}, ::Int64, ::Type{Union{Missing, String}}) at /home/<snip>/.julia/packages/SQLite/yKARA/src/tables.jl:38
[3] macro expansion at /home/<snip>/.julia/packages/SQLite/yKARA/src/tables.jl:0 [inlined]
[4] generate_namedtuple(::Type{NamedTuple{(:id, :date),Tuple{Union{Missing, Int64},Union{Missing, String}}}}, ::SQLite.Query{NamedTuple{(:id, :date),Tuple{Union{Missing, Int64},Union{Missing, String}}}}) at /home/<snip>/.julia/packages/SQLite/yKARA/src/tables.jl:43
[5] iterate at /home/<snip>/.julia/packages/SQLite/yKARA/src/tables.jl:53 [inlined]
[6] iterate at ./iterators.jl:139 [inlined]
[7] iterate at ./iterators.jl:138 [inlined]
[8] buildcolumns at /home/<snip>/.julia/packages/Tables/IT0t3/src/fallbacks.jl:93 [inlined]
[9] columns at /home/<snip>/.julia/packages/Tables/IT0t3/src/fallbacks.jl:169 [inlined]
[10] #DataFrame#404(::Bool, ::Type, ::SQLite.Query{NamedTuple{(:id, :date),Tuple{Union{Missing, Int64},Union{Missing, String}}}}) at /home/<snip>/.julia/packages/DataFrames/Iyo5L/src/other/tables.jl:34
[11] DataFrame(::SQLite.Query{NamedTuple{(:id, :date),Tuple{Union{Missing, Int64},Union{Missing, String}}}}) at /home/<snip>/.julia/packages/DataFrames/Iyo5L/src/other/tables.jl:25 [12] |>(::SQLite.Query{NamedTuple{(:id, :date),Tuple{Union{Missing, Int64},Union{Missing, String}}}}, ::Type) at ./operators.jl:813
[13] top-level scope at none:0 Here's an MWE to reproduce:
My julia version:
My best guess is that the type assertion (rightfully so) throws since the deserialization returns a DateTime, but an SQL column with type |
@Seelengrab Your assumption was correct, the way to fix this is to ensure the SQL column type is BLOB. I ran into this trying to store Measurements in a SQLite database since Measurement{Float64} is considered a "Float." I had to dig through the source, but defining the following lines was sufficient for me to able to get around the TypeError on deserialization SQLite.bind!(stmt::SQLite.Stmt, i::Int, val::Measurement) = SQLite.bind!(stmt, i, SQLite.sqlserialize(val))
SQLite.sqlitetype(::Type{T}) where {T<:Union{Missing, Measurement{Float64}}} = "BLOB" |
Oh that's a good find! I wonder if that could simply be the default fallback, though this might be problematic if more than one non-default struct is serialized that way.. |
By default, non-standard types will use |
Hi was just burned by this confusion this morning while writing a tutorial. Do we have a simple solution? Right now I'm just converting my Dates to string before writing the table. |
see discussion: https://discourse.julialang.org/t/datetimes-and-sqlite-jl-encoding/16412/3
The text was updated successfully, but these errors were encountered: