【问题标题】:Teradatasql python module not throwing duplicate errorTeradatasql python 模块不抛出重复错误
【发布时间】:2023-02-15 03:59:17
【问题描述】:
我最近安装了 teradatasql python 模块。当我对表进行批量插入时,它不会在脚本中抛出重复错误,否则它会跳过该插入语句。表的第一列在 teradata 表中为 UNIQUE。但我希望它在代码中引发错误。
with teradatasql.connect ('{"host":"whomooz","user":"guest","password":"please"}') as con:
with con.cursor () as cur:
cur.fast_executemany=True
cur.execute ("insert into voltab (?, ?)", [
[1, "abc"],
[2, "def"],
[3, "ghi"]])
【问题讨论】:
标签:
sql
python-3.x
teradata
teradata-sql-assistant
teradatasql
【解决方案1】:
您需要使用转义函数teradata_nativesql 和teradata_get_errors 来获取唯一列违规的错误。
这个示例 Python 脚本:
import teradatasql
with teradatasql.connect (host="whomooz", user="guest", password="please") as con:
with con.cursor () as cur:
cur.execute ("create volatile table voltab (c1 integer unique not null, c2 varchar(10)) on commit preserve rows")
sInsert = "insert into voltab (?, ?)"
cur.execute (sInsert, [[123, "abc"], [123, "def"]])
cur.execute ("{fn teradata_nativesql}{fn teradata_get_errors}" + sInsert)
print (cur.fetchone () [0])
打印以下输出:
[Version 17.20.0.14] [Session 1080] [Teradata Database] [Error 2801] Batch request failed to execute 1 of 2 batched statements. Batched statement 2 failed to execute because of Teradata Database error 2801, Duplicate unique prime key error in guest.voltab.
at gosqldriver/teradatasql.formatError ErrorUtil.go:89
at gosqldriver/teradatasql.(*teradataConnection).formatDatabaseError ErrorUtil.go:217
...