sqlalchemy 使用 tg2 和 pyodbc 写入 SQL Server 2005 时抛出 DataError 22001

发布于 2024-12-18 10:30:18 字数 1303 浏览 2 评论 0原文

我正在使用反射的 sqlalchemy 映射类到 SQL Server 表中。 我使用从不同源获取的数据来创建 Activity 类(映射类)的 DBSession.add() 实例。 然后我调用 transaction.commit() (因为我是从 tg2 调用的,所以我无法使用 session.commit())

错误回溯:

DataError: (DataError) ('22001', '[22001] [Microsoft][ODBC SQL Server Driver][SQ
L Server]String or binary data would be truncated. (8152) (SQLExecDirectW); [010
00] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been termin
ated. (3621)') u'INSERT INTO [NOAR_LOADEVENTS] ([EVENTCODE]) VALUES (?)' ((u'210
401',), (u'210402',), (u'210602',), (u'210603',), (u'000010',), (u'000102',), (u
'000206',), (u'000107',)  ... displaying 10 of 49 total bound parameter sets ...
  (u'211302',), (u'210403',))

很高兴获得有关此问题的任何帮助,因为我不知道如何继续/调试此问题从这里

编辑:我怀疑这与 unicode 有关,所以我将 sqlalchemy 列更改为 unicode。

也许 DataError 以某种方式卡住了,我需要调用回滚,但我不知道在 tg2 edit 中的事务中调用回滚

:mssql 中的 EVENTCODE 列是:datatype:PK, nvarchar(6),不为空 希望这有助于

另一个编辑:插入代码(相关部分)

event = #json with data
ac['EVENTCODE']=event.get('code')#for example u'210602' - from the failing data
...
...
e = Activities(**ac) # this is the class mapped with sqlalchemy to the NOAR_LOADEVENTS in sqlalchemy.
DBSession2.add(e)
transaction.commit()

I'm using a reflected sqlalchemy mapped class into a SQL Server table.
I DBSession.add() instances of Activities class (the mapped class) with data I get from a different source.
and then I called transaction.commit() (since I'm calling from tg2 I can't use session.commit())

the error traceback:

DataError: (DataError) ('22001', '[22001] [Microsoft][ODBC SQL Server Driver][SQ
L Server]String or binary data would be truncated. (8152) (SQLExecDirectW); [010
00] [Microsoft][ODBC SQL Server Driver][SQL Server]The statement has been termin
ated. (3621)') u'INSERT INTO [NOAR_LOADEVENTS] ([EVENTCODE]) VALUES (?)' ((u'210
401',), (u'210402',), (u'210602',), (u'210603',), (u'000010',), (u'000102',), (u
'000206',), (u'000107',)  ... displaying 10 of 49 total bound parameter sets ...
  (u'211302',), (u'210403',))

be glad to any help about this since I'm clueless how to continue/debug this from here

edit: I had a suspicion that this has something to do with unicode so I changed the sqlalchemy column to unicode.

maybe the DataError is stuck somehow and I need to call a rollback, but I don't know hot to call a rollback in transaction in tg2

edit:the EVENTCODE column in the mssql is:datatype:PK,nvarchar(6), not null
hope this helps

another edit: the insert code (the relevant part)

event = #json with data
ac['EVENTCODE']=event.get('code')#for example u'210602' - from the failing data
...
...
e = Activities(**ac) # this is the class mapped with sqlalchemy to the NOAR_LOADEVENTS in sqlalchemy.
DBSession2.add(e)
transaction.commit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-12-25 10:30:18

我遇到了这个错误,并通过将列(数据库端)的定义从 NVARCHAR(100) 更改为 NVARCHAR(200) 来解决它。

I jumped into this error and I solved it by changing the definition of a column (DB side) from NVARCHAR(100) to NVARCHAR(200).

杀手六號 2024-12-25 10:30:18

解决了。 @beargle 评论引导我走向正确的方向。看起来问题很奇怪,就是试图插入长度为 6 的 nvarchar 字段。似乎长度为 6 的 unicode 字符串(例如:u'110110')会产生错误,而字符串“110110”则可以正常通过。所以我使用

variable.encode('utf-8')并且一切正常。

任何。

solved. @beargle comment led me in the right direction. seems the problem strangly enough was trying to insert to nvarchar field with length of 6. seems that a unicode string with a length of 6 like: u'110110' would produce an error while a string '110110' passes fine. so i use

variable.encode('utf-8') and everything work.

what ever.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文