-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_usage.py
95 lines (82 loc) · 3 KB
/
example_usage.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# ruff: noqa: W291 Trailing whitespace
# Example script showing different types of interactions with
# CrateDB. This has no hardware dependencies so should run
# in any MicroPython environment. You will need to edit the
# code below to use your CrateDB credentials.
import cratedb
# CrateDB Docker / local network, no SSL.
crate = cratedb.CrateDB(host="localhost", use_ssl=False)
# CrateDB Cloud, using SSL.
"""
crate = cratedb.CrateDB(
host="testdrive.cratedb.net",
user="username",
password="password"
)
"""
try:
# Create a table.
print("Create table.")
response = crate.execute(
"create table driver_test(id TEXT, val1 bigint, val2 bigint, val3 boolean)"
)
# response:
# {'rows': [[]], 'rowcount': 1, 'cols': [], 'duration': 119.652275}
print(response)
# Bulk INSERT.
print("Bulk insert.")
response = crate.execute(
"insert into driver_test (id, val1, val2, val3) values (?, ?, ?, ?)",
[["a", 2, 3, True], ["b", 3, 4, False]],
)
# response:
# {'results': [{'rowcount': 1}, {'rowcount': 1}], 'cols': [], 'duration': 6.265751}
print(response)
# Basic SELECT, also returning column data types.
print("Select with column data types.")
response = crate.execute("select * from driver_test", with_types=True)
# response:
# {'col_types': [4, 10, 10, 3], 'cols': ['id', 'val1', 'val2', 'val3'], 'rowcount': 2,
# 'rows': [['b', 3, 4, False], ['a', 2, 3, True]], 'duration': 4.378391}
print(response)
# SELECT with parameter substitution.
print("Select with parameter substitution.")
response = crate.execute(
"select val1, val2 from driver_test where val1 > ? and val2 < ?", [1, 4]
)
# response:
# {'rows': [[2, 3]], 'rowcount': 1, 'cols': ['val1', 'val2'], 'duration': 3.266117}
print(response)
# INSERT with parameter substitution.
print("Insert with parameter substitution.")
response = crate.execute(
"insert into driver_test (id, val1, val2, val3) values (?, ?, ?, ?)",
["d", 1, 9, False],
)
# response:
# {'rows': [[]], 'rowcount': 1, 'cols': [], 'duration': 5.195949}
print(response)
# INSERT with parameter substitution, no parsing or return of response document.
print("Insert with parameter substitution and no response processing.")
response = crate.execute(
"insert into driver_test (id, val1, val2, val3) values (?, ?, ?, ?)",
["e", 4, 12, True],
return_response=False,
)
# response:
# None
print(response)
# Drop the table.
print("Drop table...")
response = crate.execute("drop table driver_test")
# response:
# {'rows': [[]], 'rowcount': 1, 'cols': [], 'duration': 32.84445}
print(response)
# This will throw a CrateDBError as we dropped the table.
response = crate.execute("select * from driver_test")
except cratedb.NetworkError as e:
print("Caught NetworkError:")
print(e)
except cratedb.CrateDBError as c:
print("Caught CrateDBError:")
print(c)