# ERROR: ODBC: State H: \[unixODBC\]\[

**URL:** <https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150>\
**Category:** Help!\
**Created:** [March 21, 2022, 7:40am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150 "2022-03-21T07:40:44Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 21, 2022, 7:40am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/1 "2022-03-21T07:40:44Z")

</div>

Hi, I’m using swi-prolog under macos 12.3.  
The `.odbc.ini` is :

```prolog
[ODBC Data Sources]
ftta-projects = MySQL ODBC 8.0 Unicode Driver
test = MySQL ODBC 8.0 Unicode Driver

[ODBC]
Trace = 0
TraceAutoStop = 0
TraceFile =
TraceLibrary =

[ftta-projects]
Driver = /usr/local/mysql-connector-odbc-8.0.28-macos11-x86-64bit/lib/libmyodbc8w.so

[test]
Driver = /usr/local/mysql-connector-odbc-8.0.28-macos11-x86-64bit/lib/libmyodbc8w.so
SERVER = localhost
PORT = 3306
DATABASE = ftta-projects

```

```prolog
➜ ~ odbcinst -j
unixODBC 2.3.9
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /Users/vc/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

```

```prolog
➜ ~ cat /usr/local/etc/odbcinst.ini

```

not content in `/usr/local/etc/odbcinst.ini` .

However When I call `odbc_connect(test, C, [user('root'), password('password')]).` , it reports:

```prolog
ERROR: ODBC: State H: [unixODBC][
ERROR: In:
ERROR: [10] odbc:odbc_connect(test,_3598,[user(root),...])
ERROR: [9] toplevel_call(user:user: ...) at /usr/local/lib/swipl/boot/toplevel.pl:1162

```

What is wrong with the odbc?  
Thanks very much.

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [March 21, 2022, 9:17am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/2 "2022-03-21T09:17:43Z")

</div>

I think you should try to turn tracing on. Check if [this](https://www.easysoft.com/support/kb/kb00945.html) works and writes something more into the log file (error code H is missing actual info)

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 21, 2022, 9:50am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/3 "2022-03-21T09:50:09Z")

</div>

Here is the configuration file of odbc:

```prolog
(odbc) ➜ ~ odbcinst -j
unixODBC 2.3.9
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /Users/vc/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

```

I open the `/usr/local/etc/odbcinst.ini` file, I find that it is empty.  
I add the following content to it.

```prolog
[ODBC]
Trace = yes
TraceFile = /Users/vc/odbc.log

```

Then I rerun `odbc_connect(test, C, [user('root'), password('password')]).`  
The same error still occurs, but the `odbc.log` file is not found.

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 21, 2022, 10:44am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/4 "2022-03-21T10:44:43Z")

</div>

I run the following code  
`catch(odbc_connect('test', C, [user('root'), password('password')]), E, true), write(E).` to get the exception, it returns

```prolog
error(odbc(H,1045,[unixODBC][),_130)
E = error(odbc('H', 1045, "[unixODBC]["), _).

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 21, 2022, 10:45am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/5 "2022-03-21T10:45:18Z")

</div>

The Wiki page [SWI-Prolog connecting to PostgreSQL via ODBC](https://swi-prolog.discourse.group/t/swi-prolog-connecting-to-postgresql-via-odbc/2404) might be of help. While it is not specific to your setup it does take smaller steps in verifying things along the way. Also it uses `/etc/odbc.ini` as opposed to `.odbc.ini`.

HTH

* * *

**EDIT**

In trying to identify where in the source code the error is being thrown found

> <https://github.com/SWI-Prolog/packages-odbc/blob/69c26fb71bae99eedcb8e026eb2d1e8bbe06b6de/odbc.pl#L327-L328>

Now knowing the database is [MySQL](https://www.mysql.com/) and the error number seems to be `1045` with a Google [search](https://www.google.com/search?q=mysql+error+code+1045&oq=mysql+error+code+1045) found many pages like this

> **[Fixing MySQL 1045 Error: Access Denied - Percona Database Performance Blog](https://www.percona.com/blog/2019/07/05/fixing-a-mysql-1045-error/)**
>
> In support, we often see: "I try to connect to MySQL and am getting a 1045 error". So let's take a look at why the MySQL 1045 error Access Denied triggers.

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 21, 2022, 3:57pm UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/6 "2022-03-21T15:57:42Z")

</div>

I can login into the database with `mysql -uroot -ppassword`, which seems `MySql` database allows to login via localhost. But I still cannot connect via swiprolog

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 21, 2022, 4:09pm UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/7 "2022-03-21T16:09:19Z")

</div>

> [@vancaho](#):
>
> I can login into the database with `mysql -u root -ppassword` , which seems `MySql` database allows to login via localhost. But I still cannot connect via swiprolog

Did you check

1. That you are using the correct database name. postgres uses `\l` to list the valid databases, I don’t know what MySQL command would need to be.
2. That your user id has access to the database?
3. That MySQL is listening on the correct port using netstat?
4. That you can log in using [iusql](http://manpages.ubuntu.com/manpages/trusty/man1/isql.1.html) which can check just the ODBC connect without using the SWI-Prolog part.

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 21, 2022, 5:10pm UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/8 "2022-03-21T17:10:42Z")

</div>

Yes， I have checked. The database name is correct, the userid is correct, mysql is listening on 3306, and I can query mysql with other database tools.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 21, 2022, 5:25pm UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/9 "2022-03-21T17:25:55Z")

</div>

Did you check the ODBC setup via [iusql](http://manpages.ubuntu.com/manpages/trusty/man1/isql.1.html)?

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [March 21, 2022, 7:43pm UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/10 "2022-03-21T19:43:43Z")

</div>

On Ubuntu, I have my DSNs defined in /etc/odbc.ini

```prolog
[blog]
Driver = MySQL
Description = test ODBC 
DATABASE = blog
USER = ********
PASSWORD = *********
PORT = 3306
SOCKET = /var/run/mysqld/mysqld.sock
SERVER = localhost
...

?- odbc_connect(blog,C,[]).
C = '$odbc_connection'(36329168).

```

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 23, 2022, 12:28am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/11 "2022-03-23T00:28:55Z")

</div>

It’s very strange. Today I make a new DSN which use MySQL ODBC 8.0 ANSI Driver instead of the MySQL ODBC 8.0 Unicode Driver. Now the `~/.odbc.ini` file contains two DSNs.

```prolog
➜ unixODBC-2.3.9 cat ~/.odbc.ini 
[ODBC Data Sources]
test = MySQL ODBC 8.0 Unicode Driver
test2 = MySQL ODBC 8.0 ANSI Driver

[ODBC]
TraceLibrary = 

[test]
Driver = /Library/ODBC/libmyodbc8w.so
SERVER = localhost
PORT = 3306
DATABASE = ftta-projects
USER = root
PASSWORD = *******

[test2]
Driver = /Library/ODBC/libmyodbc8a.so
SERVER = localhost
PORT = 3306
USER = root
PASSWORD = *******
DATABASE = ftta-projects

```

Then I call `odbc_connect` with the two DSNs.  
`odbc_connect(test, C, []).` reports the following error:

```prolog
ERROR: ODBC: State H: [unixODBC][
ERROR: In:
ERROR: [10] odbc:odbc_connect(test,_5560,[])
ERROR: [9] toplevel_call(user:user: ...) at /usr/local/lib/swipl/boot/toplevel.pl:1162

```

And `odbc_connect(test2, C, []).` returned successfully.

```prolog
?- odbc_connect(test2, C, []).
C = '$odbc_connection'(105553150193936).

```

So what is the difference between `MySQL ODBC 8.0 Unicode Driver` and `MySQL ODBC 8.0 ANSI Driver`? Why `MySQL ODBC 8.0 Unicode Driver` cannot be used in my projects?

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 23, 2022, 8:37am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/12 "2022-03-23T08:37:29Z")

</div>

> [@vancaho](#):
>
> Why `MySQL ODBC 8.0 Unicode Driver` cannot be used in my projects?

Who knows? It has been suggested here before: first try using the commandline tools that come with the ODBC implementation. Also note that the SWI-Prolog ODBC driver can be compiled against iODBC and UnixODBC. The pre-build binary you can download from [SWI-Prolog.org](http://SWI-Prolog.org) has been compiled against UnixODBC. The commandline tool of UnixODBC is `isql` as mentioned here before. `iusql` is its Unicode cousin. These tools have debug options that may hint at what is wrong.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 23, 2022, 9:02am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/13 "2022-03-23T09:02:28Z")

</div>

> [@vancaho](#):
>
> So what is the difference between `MySQL ODBC 8.0 Unicode Driver` and `MySQL ODBC 8.0 ANSI Driver` ? Why `MySQL ODBC 8.0 Unicode Driver` cannot be used in my projects?

Just a wild guess. It might be that the one or both the `user name` and `password` are in the wrong encoding when being passed.

On Windows I would use one of the tools from [Sysinternals](https://docs.microsoft.com/en-us/sysinternals/). As I don’t use Mac I don’t know the equivelent.

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 23, 2022, 10:09am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/14 "2022-03-23T10:09:38Z")

</div>

I prepare two DSNs, one with the ANSI driver and the other with unicode driver.

```prolog
➜ cat ~/.odbc.ini 
[ODBC Data Sources]
test = MySQL ODBC 8.0 Unicode Driver
test2 = MySQL ODBC 8.0 ANSI Driver

[ODBC]
TraceLibrary = 

[test]
Driver = /Library/ODBC/libmyodbc8w.so
SERVER = localhost
PORT = 3306
DATABASE = ftta-projects
UID = root
PWD = toorroot
USER = root
PASSWORD = ******

[test2]
Driver = /Library/ODBC/libmyodbc8a.so
SERVER = localhost
PORT = 3306
USER = root
PASSWORD = ******
DATABASE = ftta-projects

```

The `isql` command works fine with the DSN of MySQL ODBC 8.0 ANSI Driver:

```prolog
➜ isql -v test2
+---------------------------------------+
| Connected! |
| |
| sql-statement |
| help [tablename] |
| quit |
| |
+---------------------------------------+
SQL> 

```

but `iusql` fails with `test2` of MySQL ODBC 8.0 Unicode Driver:

```prolog
➜ iusql -v test
[
[ISQL]ERROR: Could not SQLDriverConnect

```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [March 23, 2022, 10:32am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/15 "2022-03-23T10:32:39Z")

</div>

The point some us are trying to make is that if you get an error from either or both [isql](https://manpages.ubuntu.com/manpages/impish/man1/isql.1.html) or [iusql](https://manpages.ubuntu.com/manpages/impish/man1/isql.1.html) then the problem is not with SWI-Prolog it is with something else.

Since the problem is not with SWI-Prolog you are more likely to get better help some where else. 🙂

---

<div class="post-metadata">

**Author:** ![vancaho](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/vancaho/32/4243_2.png) [@vancaho](https://swi-prolog.discourse.group/u/vancaho)\
**Post date:** [March 23, 2022, 10:42am UTC](https://swi-prolog.discourse.group/t/error-odbc-state-h-unixodbc/5150/16 "2022-03-23T10:42:31Z")

</div>

Thanks, SWI-Prolog now works with ANSI Driver
