odbc.py 3.3 KB
Newer Older
F
freemine 已提交
1 2 3 4 5
import pyodbc
cnxn = pyodbc.connect('DSN=TAOS_DSN;UID=root;PWD=taosdata', autocommit=True)
cnxn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
#cnxn.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
#cnxn.setencoding(encoding='utf-8')
F
freemine 已提交
6

F
freemine 已提交
7 8
cursor = cnxn.cursor()
cursor.execute("SELECT * from db.t")
F
freemine 已提交
9 10
row = cursor.fetchone()
while row:
F
freemine 已提交
11 12
    print(row)
    row = cursor.fetchone()
F
freemine 已提交
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
cursor.close()

#cursor = cnxn.cursor()
#cursor.execute("""
#INSERT INTO db.t values (?,?,?,?,?,?,?,?,?,?)
#""",
#"2020-12-12 00:00:00",
#1,
#27,
#32767,
#147483647,
#223372036854775807,
#23.456,
#899.999999,
#"foo",
#"bar")

cursor = cnxn.cursor()
cursor.execute("drop database if exists db");
cursor.close()

cursor = cnxn.cursor()
cursor.execute("create database db");
cursor.close()

cursor = cnxn.cursor()
cursor.execute("create table db.t (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))");
cursor.close()

cursor = cnxn.cursor()
cursor.execute("insert into db.t values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hello', 'world')")
cursor.close()

cursor = cnxn.cursor()
F
freemine 已提交
47
cursor.execute("insert into db.t values(?,?,?,?,?,?,?,?,?,?)", "2020-10-13 07:06:00", 0, 127, 32767, 32768, 32769, 123.456, 789.987, "hel后lo", "wo哈rld");
F
freemine 已提交
48 49 50 51
cursor.close()

cursor = cnxn.cursor()
cursor.execute("SELECT * from db.t")
F
freemine 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
row = cursor.fetchone()
while row:
    print(row)
    row = cursor.fetchone()
cursor.close()

cursor = cnxn.cursor()
cursor.execute("drop database if exists db");
cursor.close()

cursor = cnxn.cursor()
cursor.execute("create database db");
cursor.close()

cursor = cnxn.cursor()
cursor.execute("create table db.t (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(4), blob nchar(4))");
cursor.close()

cursor = cnxn.cursor()
cursor.execute("insert into db.t values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hell', 'worl')")
cursor.close()

cursor = cnxn.cursor()
cursor.execute("SELECT * from db.t")
row = cursor.fetchone()
while row:
F
freemine 已提交
78 79 80
    print(row)
    row = cursor.fetchone()
cursor.close()
F
freemine 已提交
81

F
freemine 已提交
82 83 84 85 86 87 88 89 90
cursor = cnxn.cursor()
cursor.execute("create table db.v (ts timestamp, v1 tinyint)")
cursor.close()

params = [ ('A', 1), ('B', 2), ('C', 3) ]
params = [ ('A', 1), ('B', 2), ('C', 3) ]
params = [ ('2020-10-16 00:00:00', 1),
           ('2020-10-16 00:00:01', 4),
           ('2020-10-16 00:00:02', 5),
F
freemine 已提交
91
           ('2020-10-16 00:00:03.009', 6) ]
F
freemine 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
cursor = cnxn.cursor()
cursor.fast_executemany = True
cursor.executemany("insert into db.v values (?, ?)", params)
cursor.close()

cursor = cnxn.cursor()
cursor.execute("SELECT * from db.v")
row = cursor.fetchone()
while row:
    print(row)
    row = cursor.fetchone()
cursor.close()

cursor = cnxn.cursor()
cursor.execute("SELECT * from db.v where v1 > ?", 4)
row = cursor.fetchone()
while row:
    print(row)
    row = cursor.fetchone()
cursor.close()

113 114 115 116 117 118 119 120
cursor = cnxn.cursor()
cursor.execute("SELECT * from db.v where v1 > ?", '5')
row = cursor.fetchone()
while row:
    print(row)
    row = cursor.fetchone()
cursor.close()

F
freemine 已提交
121 122 123 124 125 126 127 128 129 130
cursor = cnxn.cursor()
cursor.execute("create table db.f (ts timestamp, v1 bool)")
cursor.close()

params = [ ('2020-10-20 00:00:00', 'acb') ]
cursor = cnxn.cursor()
cursor.fast_executemany = True
cursor.executemany("insert into db.f values (?, ?)", params)
cursor.close()