sqlpython-3.xteradatateradata-sql-assistantteradatasql

Teradatasql python module not throwing duplicate error


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"]])

Solution

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