I am writing my objects to and from my database. Database columns and field names are matching, so for convenience sake I would like to get an array of fieldnames and an array of field values to pass to the SQL
INSERT INTO command. Is this the proper way of doing this task in Julia? I was hoping for some version of the
.function operator. Asking to help me learn the language better.
security = Security("sdf", "w23423")
julia> getfield(security, :CUSIP)
julia> map(field -> getfield(security, field), fieldnames(jhal.Security))
First of all, your approach seems reasonable probably similar to what I would have done as well.
There is a package which I actually found just when reseaching for this question
it is NamedTupleTools.jl
With that you could do
nt = ntfromstruct(security)
I quite like
NamedTuples, they reduce sometimes the amount of
structs one needs to define so it might be convenient for you too. But maybe it is also overkill here
(By the way, you probably know it already but it could be that there are already packages which would help you with interfacing with SQL. In particular also DataFrames.jl in case that you haven’t seen it already.)
I think you can do
I’m getting a
ERROR: MethodError: no method matching length(::Security)
when trying. Seems
doesn’t raise exception. Is this a version issue?
(I’m on 1.8.0)
Maybe. I didn’t actually test it. I forgot about how structs have a surprising default broadcasting behavior.