Skip to content

Commit 50788f1

Browse files
authored
Add SQL_ATTR_DEFERRED_PREPARE attribute support, updated README.md and INSTALL.md for ibm_db DLL loading on Windows (#1058)
* add SQL_ATTR_DEFERRED_PREPARE support and update INSTALL.md for ibm_db DLL loading on Windows Signed-off-by: Earamma K <ek@rocketsoftware.com> * Add SQL_ATTR_DEFERRED_PREPARE attribute support, updated README.md and INSTALL.md for ibm_db DLL loading on Windows Signed-off-by: Earamma K <ek@rocketsoftware.com>
1 parent 28b52c7 commit 50788f1

3 files changed

Lines changed: 41 additions & 12 deletions

File tree

INSTALL.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,47 @@ pip install ibm_db --no-binary :all: --no-cache-dir
8585

8686
- When ibm_db get installed from wheel package, you can find clidriver under site_packages directory of Python. You need to copy license file under `site_packages/clidriver/license` to be effective, if any.
8787

88-
**Note:** For windows after installing `ibm_db`, recieves the below error when we try to import ibm_db :
88+
**Windows DLL resolution (Python 3.8+):**
8989

90-
```>python
91-
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)] on win32
92-
Type "help", "copyright", "credits" or "license" for more information.
90+
Since Python 3.8, the `PATH` environment variable is no longer used for DLL resolution on Windows (see https://bugs.python.org/issue36085). You may see the following error when importing `ibm_db`:
91+
92+
```
9393
>>> import ibm_db
9494
Traceback (most recent call last):
9595
File "<stdin>", line 1, in <module>
9696
ImportError: DLL load failed while importing ibm_db: The specified module could not be found.
97-
>>>
9897
```
9998

100-
We need to make sure to set dll path of dependent library of clidriver before importing the module as:
99+
The `ibm_db` package now handles this **automatically** by installing an `ibm_db_dll.pth` file into `site-packages`. This file runs at Python startup and registers the clidriver `bin` directory via `os.add_dll_directory()`, so `import ibm_db` works out of the box.
100+
101+
If `IBM_DB_HOME` is set, the `.pth` file uses `%IBM_DB_HOME%\bin`; otherwise it uses the bundled `site-packages\clidriver\bin`.
101102

103+
**If you still see `ImportError: DLL load failed` after a fresh install**, verify that the `.pth` file exists:
104+
105+
```
106+
python -c "import os, sysconfig; print(os.path.isfile(os.path.join(sysconfig.get_path('purelib'), 'ibm_db_dll.pth')))"
107+
```
108+
109+
If it prints `False`, reinstall ibm_db:
110+
111+
```
112+
pip uninstall ibm_db
113+
pip install ibm_db
102114
```
115+
116+
**Manual fallback:** If the automatic fix does not work in your environment, you can set the DLL path directly in your code before importing the module:
117+
118+
```python
103119
import os
104120
os.add_dll_directory('path to clidriver installation until bin')
105121
import ibm_db
106-
107-
e.g:
108-
os.add_dll_directory('C:\\Program Files\\IBM\\CLIDRIVER\\bin')
109-
import ibm_db
110122
```
111123

112-
Refer https://bugs.python.org/issue36085 for more details.
124+
To find your clidriver `bin` path, run:
125+
126+
```
127+
python -c "import os, site, sysconfig; paths=[os.path.join(site.getusersitepackages(),'clidriver','bin'), os.path.join(sysconfig.get_path('purelib'),'clidriver','bin')]; print(next((p for p in paths if os.path.isdir(p)), 'clidriver not found - reinstall ibm_db'))"
128+
```
113129

114130
#### 1.2 Manual Installation
115131

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,16 @@ pip install ibm_db --no-binary :all: --no-cache-dir
121121

122122
**Windows DLL resolution (Python 3.8+):**
123123

124-
Since Python 3.8, the `PATH` environment variable is no longer used for DLL resolution on Windows (see https://bugs.python.org/issue36085). The `ibm_db` package now handles this **automatically** by installing an `ibm_db_dll.pth` file into `site-packages`. This file runs at Python startup and registers the clidriver `bin` directory via `os.add_dll_directory()`, so `import ibm_db` works out of the box.
124+
Since Python 3.8, the `PATH` environment variable is no longer used for DLL resolution on Windows (see https://bugs.python.org/issue36085). You may see the following error when importing `ibm_db`:
125+
126+
```
127+
>>> import ibm_db
128+
Traceback (most recent call last):
129+
File "<stdin>", line 1, in <module>
130+
ImportError: DLL load failed while importing ibm_db: The specified module could not be found.
131+
```
132+
133+
The `ibm_db` package now handles this **automatically** by installing an `ibm_db_dll.pth` file into `site-packages`. This file runs at Python startup and registers the clidriver `bin` directory via `os.add_dll_directory()`, so `import ibm_db` works out of the box.
125134

126135
If `IBM_DB_HOME` is set, the `.pth` file uses `%IBM_DB_HOME%\bin`; otherwise it uses the bundled `site-packages\clidriver\bin`.
127136

ibm_db.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16616,6 +16616,7 @@ static PyObject *ibm_db_get_option(PyObject *self, PyObject *args)
1661616616
case SQL_ATTR_ROWCOUNT_PREFETCH:
1661716617
case SQL_ATTR_QUERY_TIMEOUT:
1661816618
#ifndef __MVS__
16619+
case SQL_ATTR_DEFERRED_PREPARE:
1661916620
case SQL_ATTR_CALL_RETURN:
1662016621
#endif
1662116622
isInteger = 1;
@@ -19343,6 +19344,9 @@ INIT_ibm_db(void)
1934319344
PyModule_AddIntConstant(m, "SQL_ATTR_PARAMSET_SIZE", SQL_ATTR_PARAMSET_SIZE);
1934419345
PyModule_AddIntConstant(m, "SQL_ATTR_PARAM_BIND_TYPE", SQL_ATTR_PARAM_BIND_TYPE);
1934519346
PyModule_AddIntConstant(m, "SQL_PARAM_BIND_BY_COLUMN", SQL_PARAM_BIND_BY_COLUMN);
19347+
#ifndef __MVS__
19348+
PyModule_AddIntConstant(m, "SQL_ATTR_DEFERRED_PREPARE", SQL_ATTR_DEFERRED_PREPARE);
19349+
#endif
1934619350
PyModule_AddIntConstant(m, "SQL_ATTR_XML_DECLARATION", SQL_ATTR_XML_DECLARATION);
1934719351
#ifndef __MVS__
1934819352
PyModule_AddIntConstant(m, "SQL_ATTR_CLIENT_APPLCOMPAT", SQL_ATTR_CLIENT_APPLCOMPAT);

0 commit comments

Comments
 (0)