-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot parameterize SQL with strings using pyodbc #49
Comments
It seems that the driver cannot accept
This needs to be fixed. With current driver the workaround is to force PyODBC to pass string parameters in encoded (non-unicode) form, this only works with ASCII data though: cnxn.setencoding(encoding="utf-8") |
Thanks for looking into this! I've confirmed that this workaround works for me as well. Would be great to see a fix. |
Before this change the binding for `SQL_WCHAR` parameters was declared in `ParameterDescriptor::SetValue` but was not actually implemented (no encoding conversion). This change implements support for getting incoming `SQL_WCHAR`/`SQL_WVARCHAR` parameters as normal UTF-8 values. Incoming UCS-2 (on Windows) or UTF-32 (on other platforms) parameter data is converted to UTF-8 using C++ stdlib. Testing: added a test that inserts non-ASCII data as literal and then uses wide char parameter binding to match this data in `SELECT`. Fixes: duckdb#49
This change adds only a nominal support for passing query parameters as `SQL_WVARCHAR`, it uses existing logic that is already there for `SQL_WCHAR` parameters. It allows to use parameters from PyODBC, but in current impl only ASCII data can be passed in these parameters. Testing: none, test suite needs to be extended to test unicode through the driver manager. Fixes: duckdb#49
Just FYI, this appeared not to be the case, only ASCII is working correctly. I cannot see any easy way to enable support for non-ASCII data, filed #69 for that. |
@staticlibs thanks for the heads up. This fix should be helpful for me even if it's not as complete a solution as previously intended. Thank you! |
What happens?
Executing parameterized SQL with pyodbc fails when the parameters are provided as python strings, but works as expected when parameters are provided as numeric values.
To Reproduce
Execute a parameterized query via pyodbc and specify the parameter as a string:
This produces the following error:
To confirm that it does work with numeric values:
Outputs
[(42,)]
as expected.OS:
Windows 11 x64 version 10.1.26100
DuckDB Version:
v1.1.3 (nightly build as of 2024-12-29)
DuckDB Client:
ODBC
Hardware:
Intel Core i7-9750H CPU @ 2.6GHz × 6 , 16 GB Memory
Python environment:
Anaconda Python version 3.13.1, pyodbc version 5.2.0
Name:
Matthew T. Slaughter
Affiliation:
Kaiser Permanente
The text was updated successfully, but these errors were encountered: