-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathExportCSVResult.py
36 lines (29 loc) · 1.14 KB
/
ExportCSVResult.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Copyright 2021 by Teradata Corporation. All rights reserved.
# This sample program demonstrates how to export the results from a select statement into a csv file.
import csv
import os
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, c2 varchar(100)) on commit preserve rows")
print ("Inserting data")
cur.execute ("insert into voltab values (?, ?)", [
[1, ""],
[2, "abc"],
[3, "def"],
[4, "mno"],
[5, ""],
[6, "pqr"],
[7, "uvw"],
[8, "xyz"],
[9, ""],
])
sFileName = "dataPy.csv"
print ("Exporting table data to file", sFileName)
cur.execute ("{fn teradata_write_csv(" + sFileName + ")}select * from voltab order by 1")
try:
print ("Reading file", sFileName)
with open (sFileName, "rt", encoding="UTF8") as f:
[ print (row) for row in csv.reader (f) ]
finally:
os.remove (sFileName)