File size: 2,705 Bytes
2b4d75c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# WaveDB
# How to use WaveDB, a simple sqlite3 database server that ships with Wave.
# ---

# Before you run this example, start WaveDB (`wavedb`).
# By default, WaveDB listens on port 10100.
#
# To run this example, execute `python db.py`
#
# If your WaveDB instance is configured differently, you might want to set
#   the following environment variables accordingly:
# H2O_WAVEDB_ADDRESS - the ip:port of the database server
# H2O_WAVEDB_ACCESS_KEY_ID - the API access key ID
# H2O_WAVEDB_ACCESS_KEY_SECRET - the API access key secret

import asyncio
from h2o_wave import connect


async def main():
    # Create a database connection
    connection = connect()

    # Access the 'employees' database.
    # A new database is created automatically if it does not exist.
    db = connection["employees"]

    # Execute some statements.
    await db.exec("drop table if exists employee")
    await db.exec("create table employee(empid integer, name text, title text)")

    # Execute a statement and handle errors.
    results, err = await db.exec("insert into employee values(?, ?, ?)", 101, 'Jeffrey Lebowski', 'Slacker')
    if err:
        raise ValueError(err)

    # Execute many statements.
    insert_employee = "insert into employee values(?, ?, ?)"
    await db.exec_many(
        (insert_employee, 102, 'Walter Sobchak', 'Veteran'),
        (insert_employee, 103, 'Donny Kerabatsos', 'Sidekick'),
        (insert_employee, 104, 'Jesus Quintana', 'Bowler'),
        (insert_employee, 105, 'Uli Kunkel', 'Nihilist'),
    )

    # Execute many statements as a transaction.
    await db.exec_atomic(
        (insert_employee, 106, 'Brandt', 'Butler'),
        (insert_employee, 107, 'Maude Lebowski', 'Artist'),
        (insert_employee, 108, 'Franz', 'Nihilist'),
        (insert_employee, 109, 'Kieffer', 'Nihilist'),
    )

    # Read records.
    rows, err = await db.exec("select * from employee")
    if err:
        raise ValueError(err)

    print(rows)

    # Prints:
    #  [
    #     [101, 'Jeffrey Lebowski', 'Slacker'],
    #     [102, 'Walter Sobchak', 'Veteran'],
    #     [103, 'Donny Kerabatsos', 'Sidekick'],
    #     [104, 'Jesus Quintana', 'Bowler'],
    #     [105, 'Uli Kunkel', 'Nihilist'],
    #     [106, 'Brandt', 'Butler'],
    #     [107, 'Maude Lebowski', 'Artist'],
    #     [108, 'Franz', 'Nihilist'],
    #     [109, 'Kieffer', 'Nihilist']
    # ]

    # Clean up.
    await db.exec("drop table employee")

    # Drop the database entirely. Warning: A database is irrecoverable once dropped.
    await db.drop()

    # Close connection.
    await connection.close()


loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(main())