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
4.0.22 MSSQL Exceptions: Restricted data type attribute violation and Data Truncation #334
Comments
re: your first exampleYour parameter values are in the wrong order. In your gist you define your table as curs.execute('''CREATE TABLE test_insert (
id smallint not null,
val smalldatetime not null,
id2 smallint not null,
id3 [smallint] NOT NULL,
id4 [smallint] NOT NULL,
id5 [tinyint] NOT NULL,
id6 [tinyint] NOT NULL,
id7 [tinyint] NOT NULL,
id8 [smallint] NOT NULL
)''') and in your first example
you are trying to insert re: your second exampleI cannot reproduce the issue under pyodbc 4.0.22 using this code: cnxn = pyodbc.connect(conn_str, autocommit=True)
crsr = cnxn.cursor()
crsr.execute('''CREATE TABLE #test_insert (
id smallint not null,
val smalldatetime not null,
id2 smallint not null,
id3 [smallint] NOT NULL,
id4 [smallint] NOT NULL,
id5 [tinyint] NOT NULL,
id6 [tinyint] NOT NULL,
id7 [tinyint] NOT NULL,
id8 [smallint] NOT NULL
)''')
stmt = 'INSERT INTO #test_insert (id8,val,id6,id7,id4,id5,id2,id3,id) VALUES (?,?,?,?,?,?,?,?,?)'
data = [2005, u'2005-01-01 00:00:00', 1, 1, 1, 7, 1, 1, 1]
crsr.execute(stmt, data) |
Here is an updated gist that definitely fails on my PC And the sql trace |
I can confirm that this is an issue with pyodbc 4.0.22. My repro code: import pyodbc
import sys
print('Python version ' + sys.version.replace('\n', ' '))
# Python version 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:25:58) [MSC v.1500 64 bit (AMD64)]
print('pyodbc version ' + pyodbc.version)
conn = pyodbc.connect(
r'DRIVER=ODBC Driver 11 for SQL Server;'
r'SERVER=.\SQLEXPRESS;'
r'DATABASE=myDb;'
r'Trusted_Connection=yes;'
)
curs = conn.cursor()
table_name = 'test_insert'
curs.execute("SELECT name FROM sys.tables where name = ?", table_name)
if curs.fetchone():
curs.execute('DROP TABLE ' + table_name)
print('(Table [{}] dropped.)'.format(table_name))
curs.execute('''CREATE TABLE {} (
id smallint not null,
val smalldatetime not null
)'''.format(table_name))
curs.commit()
print('(Table [{}] created.)'.format(table_name))
stmt = 'INSERT INTO {} (val, id) VALUES (?,?)'.format(table_name)
print('stmt: ' + repr(stmt))
data = (u'2005-01-01 00:00:00', 1)
print('data: ' + repr(data))
curs.execute(stmt, data) The above works fine under 4.0.21 but fails under 4.0.22 with the error
Interestingly, under 4.0.22 |
That trace looks odd --- I do not see any execution of the queries, nor do any of the functions called return SQL_ERROR. The first time it does SQLPrepare, SQLNumParams, and SQLDescribeParameter for all the parameters (and the returned types are correct), but then instead of binding it immediately frees the statement. The second time it prepares, describes, and binds, but frees the statement without executing it. |
Hello. I'm with the same problem. Anyone have a suggestion? |
@WilliamZimmermann You could revert to pyodbc 4.0.21 for the time being. |
Hello @gordthompson . Thank you. I did it and now, with 4.0.21 it's working again. Thank you very much. |
@v-chojas - The 4.0.22 issue(s) also affect parameterized queries trying to insert strings > 255 characters into an Access "Long Text" (a.k.a. "Memo") column as described in the Stack Overflow question here. I have confirmed that 4.0.22 fails while 4.0.21 succeeds. If you'd prefer that I create a separate issue for this please let me know. |
Please upgrade to 4.0.23 which reverts the problematic commits from 4.0.22. |
I have 4.0.23 and have the same issue @mkleehammer |
I was using 4.0.22, and facing this error. After upgrading to 4.0.23, this error is gone. |
I fixed my issue by replacing all np.nan values to standard python None value. MSSQL doesn't seem to understand np.nan, hence the data type violation. See this for how to replace np.nan with None. |
Could be an unrelated issue but changing the driver to a more modern one did the trick for me. |
Environment
To diagnose, we usually need to know the following, including version numbers. On Windows, be
sure to specify 32-bit Python or 64-bit:
Issue
There are multiple errors while trying to insert data using cursor.execute that appeared in 4.0.22. This repro script should help test: https://gist.github.com/brewneaux/4f8c52865b9f2ad4f273cd3da000a575
The text was updated successfully, but these errors were encountered: