When I try something seemingly straight forward like:
import db_odbc
var db = open("100.10.10.10", "user", "password", "dbname")
echo "Worked"
db.close()
I get the error:
db_common.nim(100) dbError
Error: unhandled exception: Error: unable to set ODBC driver version. [DbError]
I've tried searching online and even looking through the code but I can't really understand what I need to do to set the appropriate driver version.
Any suggestions would be very helpful or example of someone using the library with ms sql server.
Thanks for the suggestion @shirleyquirk
When I look here:
It appears that I might get an init error first if the db returned SQL_SUCCESS_WITH_INFO:
# allocate environment handle
var res = SQLAllocHandle(SQL_HANDLE_ENV, result.env, result.env)
if res != SQL_SUCCESS: dbError("Error: unable to initialise ODBC environment.")
res = SQLSetEnvAttr(result.env,
SQL_ATTR_ODBC_VERSION.TSqlInteger,
cast[SqlPointer](val.addr), resLen.TSqlInteger)
if res != SQL_SUCCESS: dbError("Error: unable to set ODBC driver version.")
# allocate hDb handle
Do you have the odbc driver installed?
you could try editing that line in your local db_odbc.nim to accept SUCCESS_WITH_INFO and/or give you more error info on failure.
if (res and not 1)!=0: dbError("Error: unable to initialise ODBC enviroment.")
#or
#if not (res in [SQL_SUCCESS,SQL_SUCCESS_WITH_INFO]):
how to get the error code out is with SqlGetDiagRec but god that looks like actual work. I downloaded and installed (full install including the sdk): Download Microsoft ODBC Driver 17 for SQL Server (x64) I tried also installing older versions but still received the same error.
I've also tried the odbc nim library.
import odbc
var
con = newODBCConnection()
con.driver = "SQL Server Native Client 11.0"
con.host = "x.xx.xx.xxx"
con.database = "xxxxx"
con.integratedSecurity = false
con.userName = "xxxxxxx"
con.password = "xxxxxxx"
if not con.connect:
echo "Could not connect to database."
With this library I see:
src\gitlab.com\nim-sqlserver\odbc\odbc\odbchandles.nim(44, 35) Error: type mismatch: got <SqlHEnv, TSqlInteger, TSqlInteger, int literal(0)>
but expected one of:
proc SQLSetEnvAttr(EnvironmentHandle: SqlHEnv; Attribute: TSqlInteger;
Value: SqlPointer; StringLength: TSqlInteger): TSqlSmallInt
first type mismatch at position: 3
required type for Value: SqlPointer
but expression 'odbcVersion' is of type: TSqlInteger
expression: SQLSetEnvAttr(envHandle, TSqlInteger(200), odbcVersion, 0)
@shirleyquirk
I'll try that branch to see if it works.
On nim version 1.2.0 I was able to get things working. Part of the issue had to do with configurations on my end with the drivers. The coded db errors msgs I received when using 1.2.0 helped me sort it out. I tried the working script using 1.2.6 to see if it was me all along but it still gives the same error so there is an issue.
@shirleyquirk
Just now switched over to devel branch. Built the same file that I've confirmed works on 1.2.0 but I see the same error:
db_common.nim(100) dbError
Error: unhandled exception: Error: unable to set ODBC driver version. [DbError]
I was able to replicate your error, on Linux, using microsoft sql-server, and the ms odbc driver. working on 1.2.0, not working on 1.2.6.
but bleeding edge devel did fix it for me. Its not enough to choosenim devel, you need to choosenim devel --latest if you use choosenim.
if the install didn't have to compile, it's too old lol i.e. nim -v should say 2020-09-29
or wait a day and get the next nightly build :)
(also, installing and setting up microsoft sql server? lord have mercy there was swearing XD
Hehe, Yeah I'd be swearing as well... Thankfully this is only for very basic applications...
I can confirm it 'works' on the latest devel branch.
Thanks for all the help with this!