I'm having a problem with getting data out of a sqlite database. If I comment the offending statements, it compiles and 'strings SqliteDB' shows the character data just fine, so I think most of the program is correct. If I use postgresql (with slightly different syntax), getting data out with a 'for x in db.fastdata(sqlQuery):` works.
I get the same error using either the 14.2 or 15.2 compilers. CXX or Clang doesn't seem to make a difference.
# sqlite.nim
import db_sqlite
# import db_postgres
import strutils, strfmt, sequtils
import tables, hashes, math
let dbx = open("SqliteDB", nil, nil, nil)
dbx.exec(sql"Drop table if exists myTestTbl")
dbx.exec(sql("""create table myTestTbl (
Id INTEGER PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
i INT(11),
f DECIMAL(18,10))"""))
dbx.exec(sql"INSERT INTO myTestTbl (id, Name, i, f) VALUES (1, ?, ?, ?)",
"Alex", 200, 40.11)
for x in exec(dbx,sql"SELECT * FROM myTestTbl"):
echo x.repr
dbx.close()
Below shows the compiler diagnostic
air:dataquery bobgus$ nim c -r sqlite.nim
sqlite.nim(17, 14) Error: expression 'exec(dbx,
SqlQuery(r"SELECT * FROM myTestTbl"),
[])' has no type (or is ambiguous)
Below shows the nim version
$ nim -V
Nim Compiler Version 0.15.2 (2016-10-22) [MacOSX: amd64]
Copyright (c) 2006-2016 by Andreas Rumpf
git hash: 9f895c6f5a4133f6c06324be3f4032b273a48971
active boot switches: -d:release
for x in exec(dbx,sql"SELECT * FROM myTestTbl"):
should work. I think exec is a proc without a return value, so you can not iterate with "for x in". You may try the iterators like rows or maybe a proc with a return value, maybe a string. I guess my response is not too helpful for you -- I have never used databases in Nim yet.
From http://nim-lang.org/docs/db_sqlite.html#examples-larger-example
for x in theDb.fastRows(sql"select * from myTestTbl"):
echo x
Ok, thanks much. I should have inspected that example a bit more carefully.
(Just take out the 'exec' and make the line more like a postgresql SELECT command..)