英文:
Teradatasql python module not throwing duplicate error
问题
我最近安装了TeradataSQL Python模块。当我批量插入数据到表中时,脚本没有抛出重复错误,而是跳过了该插入语句。在Teradata表中,表的第一列是唯一的。但我希望在代码中引发一个错误。
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"]])
英文:
I have installed teradatasql python module recently.When I am doing batch insert into table it is not throwing duplicate error in the script, Else it is skipping that insert statement. Table has first column as UNIQUE in teradata table. But I want it to throw an error in the code.
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"]])
答案1
得分: 0
你需要使用转义函数 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
...
英文:
You need to use the escape functions teradata_nativesql
and teradata_get_errors
to obtain errors for unique column violations.
This example Python script:
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])
Prints the following output:
[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
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论