Enjoying the heck out of nim so far.

In case it's useful for documentation or anything...

So I want to use db, but it seems db_sqlite doesn't really offer prepared statements (though it uses them internally), so I dip down into sqlite3. A few hours for total noob to have prepared statement working, not bad. Here's what caused bother/confusion along the way:

  • Sources could be more cross linked, i.e. types between pages, sources to each other etc. Quite a lot of flipping between db_sqlite, sqlite3, and db_common. Took a google to find db_common because for some reason it doesn't show up on modules page. db_common seems maybe mis-named as it appears to have some SQL-specific stuff, though maybe its more generally useful I don't know.
  • Reinvented dbError due to stupidity, which was a bit painful because I couldn't figure out how to make a string from a cstring. After some stack exchange I ended up with toString like so:

import sequtils

proc toString(str: seq[char]): string =
  result = newStringOfCap(len(str))
  for ch in str:
    add(result, ch)

proc dbErrExcept(): ref Exception =
  var escs: cstring = db.errmsg
  var es = toString(toSeq(escs.items))
  result = newException(Exception, es)

It seemed like there must be an easier way to go cstring -> string but it took a long time to realize it was $ operator. Not sure what might be done to make realizing this more likely, maybe it's all me.

2017-10-11 07:14:24
About toString - in Nim $ is used as a stringify operator for almost all types anywhere (not only in stdlib). So you should firstly try $ on a type you want to convert to string
2017-10-11 10:45:58

SqLite is written in C

SqLite therefore uses cstring

Don't bother with seq[char], just use cstring which Nim easily converts to string


Why not copy what db_sqlite has done, and have your own setupQuery() which calls SqLite's prepare_v2()

then you can

var stmt = setupQuery(db, query, args)
# now step thru the stmt to iterate thru rows of data

but don't let me stop you doing things the hard way

2017-10-11 23:54:35
When I have a question on how to do something in Nim, I often search Rosetta Code. 2017-10-12 05:05:28