Skip to content

Commit 68e47df

Browse files
committed
Add hello-covenantsql demo
1 parent ddb9243 commit 68e47df

File tree

6 files changed

+330
-0
lines changed

6 files changed

+330
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ venv.bak/
102102

103103
# mypy
104104
.mypy_cache/
105+
106+
# Project specified
107+
.vscode/

Pipfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
pycovenantsql = "*"
8+
9+
[dev-packages]
10+
pylint = "*"
11+
yapf = "*"
12+
13+
[requires]
14+
python_version = "3.6"

Pipfile.lock

Lines changed: 178 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# CovenantSQL Python Demos
2+
3+
This project contains several domos for demonstrating how to utilize CovenantSQL's python driver.
4+
5+
## 1. Setup
6+
7+
```bash
8+
git clone https://github.com/CovenantSQL/python-demos.git
9+
cd python-demos
10+
pipenv install
11+
```
12+
13+
## 2. Run a Demo
14+
15+
```bash
16+
pipenv shell
17+
cd hello-covenantsql
18+
chmod u+x main.py
19+
./main.py
20+
```

hello-covenantsql/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# hello-covenantsql
2+
3+
CovenantSQL's "hello world" demo.
4+
5+
## Usage
6+
7+
```bash
8+
./main.py
9+
```
10+
11+
Expected output:
12+
13+
```
14+
$ ./main.py
15+
create table
16+
insert sample data
17+
affected rows: 1, lastrowid: 1
18+
select data from the table
19+
(1, '2018-11-06T18:21:23Z', 'Apple', 'appleisdelicious')
20+
21+
$ ./main.py
22+
create table
23+
insert sample data
24+
affected rows: 1, lastrowid: 2
25+
select data from the table
26+
(1, '2018-11-06T18:21:23Z', 'Apple', 'appleisdelicious')
27+
(2, '2018-11-06T18:21:35Z', 'Apple', 'appleisdelicious')
28+
```
29+
30+
**NB:** you can tweak database configurations through environment variables:
31+
32+
```bash
33+
COVENANTSQL_HOST=192.168.2.xxx COVENANTSQL_PORT=12345 ./main.py
34+
```
35+
36+
Available environment variables are:
37+
38+
- **COVENANTSQL_HOST:** domain of the CovenantSQL adapter service;
39+
- **COVENANTSQL_PORT:** port of the CovenantSQL adapter service;
40+
- **COVENANTSQL_PRIVATE_KEY:** path of the private key file;
41+
- **COVENANTSQL_PROXY_PEM:** path of the proxy pem file;
42+
- **COVENANTSQL_DATABASE:** database name (hash).

hello-covenantsql/main.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python3
2+
"""
3+
PyCovenantSQL driver demo: hello-covenantsql
4+
"""
5+
6+
import os
7+
import pycovenantsql
8+
9+
DB_CONFIG = {
10+
'host':
11+
os.getenv('COVENANTSQL_HOST', '127.0.0.1'),
12+
'port':
13+
int(os.getenv('COVENANTSQL_PORT', '11105')),
14+
'key':
15+
os.getenv(
16+
'COVENANTSQL_PRIVATE_KEY',
17+
'./test/service/node_c/admin.test.covenantsql.io-key.pem',
18+
),
19+
'https_pem':
20+
os.getenv(
21+
'COVENANTSQL_PROXY_PEM',
22+
'./test/service/node_c/admin.test.covenantsql.io.pem',
23+
),
24+
'database':
25+
os.getenv(
26+
'COVENANTSQL_DATABASE',
27+
'057e55460f501ad071383c95f691293f2f0a7895988e22593669ceeb52a6452a',
28+
),
29+
}
30+
31+
32+
def main():
33+
"""
34+
Main program
35+
"""
36+
37+
conn = pycovenantsql.connect(**DB_CONFIG)
38+
with conn.cursor() as cursor:
39+
# Create a new table
40+
sql_create_table = """
41+
CREATE TABLE IF NOT EXISTS `users` (
42+
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
43+
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
44+
`email` VARCHAR(255) NOT NULL,
45+
`password` VARCHAR(255) NOT NULL
46+
);
47+
"""
48+
print('create table')
49+
cursor.execute(sql_create_table)
50+
51+
# Insert some data
52+
print('insert sample data')
53+
sql_insert_data = """INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"""
54+
affected_rows = cursor.execute(
55+
sql_insert_data,
56+
('Apple', 'appleisdelicious'),
57+
)
58+
print('affected rows: %d, lastrowid: %d' % (
59+
affected_rows,
60+
cursor.lastrowid,
61+
))
62+
63+
# Query data
64+
print('select data from the table')
65+
sql_select_data = """SELECT * FROM `users`"""
66+
cursor.execute(sql_select_data)
67+
for row in cursor:
68+
print(row)
69+
conn.close()
70+
71+
72+
if __name__ == '__main__':
73+
main()

0 commit comments

Comments
 (0)