mkleehammer / pyodbc

Python ODBC bridge
https://github.com/mkleehammer/pyodbc/wiki
MIT No Attribution
2.92k stars 561 forks source link

Unable to use ¶ in like statement #1086

Closed pljspahn closed 1 year ago

pljspahn commented 2 years ago

Environment

Issue

I want to query a field that contains a ¶ character. A basic example of this would be such as:

sql = """
    select
        prod_code,
        prod_loc
    from product
    where prod_loc like '%¶%'
"""

This returns no results while a value in prod_loc is stored like:

&102D&n365 ¶

I set my connection up as:

import pyodbc
from os import environ

class EloquenceConnect:
    cursor = None

    def __init__(self):
        connection = pyodbc.connect('DSN='+environ['ODBC_DSN'])
        connection.setencoding(encoding='utf-8')
        self.cursor = connection.cursor()

I suspect there's some mishap with how I have set up the connection's encoding, or maybe I need to set the decoding as well? Any suggestions on how to handle this?

v-chojas commented 2 years ago

You're right that this likely has something to do with encoding. I suggest retrieving the field from the database through the driver, inspecting the byte sequence to determine the encoding it returned the data in, and using that in sending data to the driver.