【Python连接数据库】Python连接Teradata数据库-TD SQL Driver 方式(teradatasql包)

1.Python安装

详见文档:Python连接Teradata数据库-ODBC方式

 2.使用teradatasql连接TD数据库

Teradata SQL驱动(Python)下载地址:teradatasql-16.20.0.42-py3-none-any.whl

(1)使用pip安装或升级(macOS or Linux系统)

pip install teradatasql-16.20.0.42-py3-none-any.whl   #安装
pip install --no-cache-dir -U teradatasql  #升级

安装teradatasql时,出现错误Could not find a version that satisfies the requirement pycryptodome.可参考如下文档解决:安装teradatasql提示找不到pycryptodome模块错误

(2)使用pip安装或升级(Windows系统)

py -3 -m pip install teradatasql-16.20.0.42-py3-none-any.whl #安装 
py -3 -m pip install --no-cache-dir -U teradatasql #升级

(3)验证安装成功

安装完毕后,在Python安装目录/usr/local/python3/下,新增teradatasql文件夹(测试py文件)。在第三方包目录/usr/local/python3/lib/python3.6/site-packages 下新增teradatasql文件夹即为成功。

(4)测试脚本

/usr/local/python3/teradatasql/samples/BatchInsert.py

# Copyright 2018 by Teradata Corporation. All rights reserved.

# This sample program demonstrates how to insert a batch of rows.

import teradatasql

with teradatasql.connect ('{"host":"192.168.253.131","user":"dbc","password":"dbc"}') as con:
    with con.cursor () as cur:
        cur.execute ("create volatile table voltab (c1 integer, c2 varchar(100)) on commit preserve rows")

        cur.execute ("insert into voltab (?, ?)", [
            [1, "abc"],
            [2, "def"],
            [3, "ghi"]])

        cur.execute ("select * from voltab order by 1")
        [ print (row) for row in cur.fetchall () ]

执行结果如下:

 

 

官方说明文档:

Teradata SQL Driver for Python

posted @ 2019-07-23 20:12  李子恒  阅读(1784)  评论(0编辑  收藏  举报