The error is coming from the driver manager. You will be calling ODBC functions out of sequence. Turn on driver manager tracing, in your odbcinst.ini add
[ODBC]
Trace = Yes
TraceFile = /tmp/sql.log
Then run your app and see what lead up to the error.