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

F
freemine 已提交
8 9 10 11 12 13 14
#cursor = cnxn.cursor()
#cursor.execute("SELECT * from db.t")
#row = cursor.fetchone()
#while row:
#    print(row)
#    row = cursor.fetchone()
#cursor.close()
F
freemine 已提交
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

#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()
F
freemine 已提交
40
cursor.execute("create table db.mt (ts timestamp, b bool, v1 tinyint, v2 smallint, v4 int, v8 bigint, f4 float, f8 double, bin binary(40), blob nchar(10))");
F
freemine 已提交
41 42 43
cursor.close()

cursor = cnxn.cursor()
F
freemine 已提交
44
cursor.execute("insert into db.mt values('2020-10-13 06:44:00', 1, 127, 32767, 32768, 32769, 123.456, 789.987, 'hello', 'world')")
F
freemine 已提交
45 46 47
cursor.close()

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

cursor = cnxn.cursor()
F
freemine 已提交
52
cursor.execute("SELECT * from db.mt")
F
freemine 已提交
53 54 55 56 57 58
row = cursor.fetchone()
while row:
    print(row)
    row = cursor.fetchone()
cursor.close()

F
freemine 已提交
59 60 61 62 63 64 65
#cursor = cnxn.cursor()
#cursor.execute("drop database if exists db");
#cursor.close()
#
#cursor = cnxn.cursor()
#cursor.execute("create database db");
#cursor.close()
F
freemine 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78

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 已提交
79 80 81
    print(row)
    row = cursor.fetchone()
cursor.close()
F
freemine 已提交
82

F
freemine 已提交
83 84 85 86 87 88 89 90 91
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 已提交
92
           ('2020-10-16 00:00:03.009', 6) ]
F
freemine 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
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()

114 115 116 117 118 119 120 121
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 已提交
122
cursor = cnxn.cursor()
F
freemine 已提交
123
cursor.execute("create table db.f (ts timestamp, v1 float)")
F
freemine 已提交
124 125
cursor.close()

F
freemine 已提交
126
params = [ ('2020-10-20 00:00:10', '123.3') ]
F
freemine 已提交
127 128 129 130 131
cursor = cnxn.cursor()
cursor.fast_executemany = True
cursor.executemany("insert into db.f values (?, ?)", params)
cursor.close()