anthony-tuininga / ceODBC

Python module for accessing databases using the ODBC API.
https://anthony-tuininga.github.io/ceODBC/
12 stars 8 forks source link

String data, right truncation #14

Open antonlahti opened 1 year ago

antonlahti commented 1 year ago

EDIT: I created a new issue instead of commenting on a closed one...

EDIT: I'm running:

EDIT: Added ODBC trace: SQL.LOG

Given this table declaration:

CREATE TABLE strings (
    id int NOT NULL,
    [data] nvarchar(400) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
    CONSTRAINT PK__strings__3213E83FDE156BB0 PRIMARY KEY (id),
    CONSTRAINT UQ__strings__D9DE21E1EE0369BF UNIQUE ([data])
);

And the following Python code::

import ceODBC

dsn = "DRIVER={ODBC Driver 13 for SQL Server};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s" % (
    server,
    database,
    uid,
    pwd,
)
db_connection = ceODBC.connect(dsn)
cursor = db_connection.cursor()
cursor.execute("INSERT INTO strings VALUES (1, ?)", "²")

I get the following error:

Traceback (most recent call last): File "mini.py", line 13, in cursor.execute("INSERT INTO strings VALUES (1, ?)", "²") File "src\ceODBC\cursor.pyx", line 423, in ceODBC.driver.Cursor.execute File "src\ceODBC\cursor.pyx", line 268, in ceODBC.driver.Cursor._execute File "src\ceODBC\errors.pyx", line 31, in ceODBC.driver._check_stmt_error File "src\ceODBC\errors.pyx", line 23, in ceODBC.driver._check_error File "src\ceODBC\errors.pyx", line 67, in ceODBC.driver._raise_from_odbc File "src\ceODBC\errors.pyx", line 83, in ceODBC.driver._raise_from_string ceODBC.exceptions.DatabaseError: [Microsoft][ODBC Driver 13 for SQL Server]String data, right truncation

anthony-tuininga commented 6 months ago

I don't have access to SQL Server, but it sounds like an encoding issue. Can you specify the use of UTF-8 in the connect string and does that resolve the issue?