on linux, unicode parameters cannot be longer than 128
See original GitHub issueI noticed the following when posting a query from linux using parameters:
If the parameter string is longer than 127 characters, pypyodbc will throw an error on row 1572 when trying this:
param_buffer.value = c_char_buf
It complains that c_char_buf is too large.
I checked: c_char_buf is created by UCS_buf() if parameter type is ‘u’ (unicode less than 255 characters):
c_char_buf = UCS_buf(param_val)
c_buf_len = len(c_char_buf)
UCS_buf(string) is defined to return encoding(utf-16-le) for linux, and return its argument for windows.
Thus on linux, due to 2 byte minimum char width for utf-16, the length of the byte array will be double the length of the underlying string.
As param_buffer.value size is defined as 255 no matter what platform, this leads to above error.
My fix was to double the buffer size for linux:
if param_types[col_num][0] == 'u':
sql_c_type = SQL_C_WCHAR
sql_type = SQL_WVARCHAR
if sys.platform not in ('win32','cli'):
buf_size = 255 * 2 # double buffer size for linux
else:
buf_size = 255
ParameterBuffer = create_buffer_u(buf_size)
Hope this helps someone.
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)

Top Related StackOverflow Question
An alternative that works for me is to pass encoded bytes as parameters. I’m working with SQL Server and pypyodbc 1.3.4.3
For example:
I purposefully left the byte-trunacting in (ie: [:32]) because that works properly.
Related, see PR: https://github.com/jiangwen365/pypyodbc/pull/73/files