forked from exasol/pyexasol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a03_fetch_dict.py
47 lines (33 loc) · 1.19 KB
/
a03_fetch_dict.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
"""
Fetch rows as dictionaries
"""
import pyexasol
import _config as config
import pprint
printer = pprint.PrettyPrinter(indent=4, width=140)
# Basic connect (default mapper)
C = pyexasol.connect(dsn=config.dsn, user=config.user, password=config.password, schema=config.schema, fetch_dict=True)
# Fetch tuples row-by-row as iterator
stmt = C.execute("SELECT * FROM users ORDER BY user_id LIMIT 5")
for row in stmt:
printer.pprint(row)
# Fetch tuples row-by-row with fetchone
stmt = C.execute("SELECT * FROM users ORDER BY user_id LIMIT 5")
while True:
row = stmt.fetchone()
if row is None:
break
printer.pprint(row)
# Fetch many
stmt = C.execute("SELECT * FROM users ORDER BY user_id LIMIT 5")
printer.pprint(stmt.fetchmany(3))
printer.pprint(stmt.fetchmany(3))
# Fetch everything in one go
stmt = C.execute("SELECT * FROM users ORDER BY user_id LIMIT 5")
printer.pprint(stmt.fetchall())
# Fetch one column as list of values (same as tuples example!)
stmt = C.execute("SELECT user_id FROM users ORDER BY user_id LIMIT 5")
printer.pprint(stmt.fetchcol())
# Fetch single value (same as tuples example!)
stmt = C.execute("SELECT count(*) FROM users")
printer.pprint(stmt.fetchval())