Teradatasql Python模块未抛出重复错误

huangapple go评论81阅读模式
英文:

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_nativesqlteradata_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
 ...

huangapple
  • 本文由 发表于 2023年2月14日 22:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75449290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定