This repository was archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.py
69 lines (56 loc) · 1.82 KB
/
test.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
#!/usr/bin/env python
# coding:utf-8
# Author: [email protected]
import asyncio
from sanicdb import SanicDB
async def test(loop):
db = SanicDB('localhost', 'testdb', 'root', 'the_password',
minsize=3, maxsize=5,
connect_timeout=5,
loop=loop)
sql = 'Drop table test'
await db.execute(sql)
sql = """CREATE TABLE `test` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`name` varchar(16) NOT NULL,
`content` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM ;"""
await db.execute(sql)
sql = 'select * from test where name = %s'
data = await db.query(sql, 'abc')
print('query():', data)
sql += ' limit 1'
d = await db.get(sql, 'abc')
print('get():', d)
sql = 'delete from test where name=%s'
lastrowid = await db.execute(sql, 'xyz')
print('execute(delete...):', lastrowid)
sql = 'insert into test set name=%s, content=%s'
lastrowid = await db.execute(sql, 'xyz', '456')
print('execute(insert...):', lastrowid)
ret = await db.table_has('test', 'name', 'abc')
print('has(): ', ret)
ret = await db.table_update('test', {'content': 'updated'},
'name', 'abc')
print('update():', ret)
sql = 'select * from test where name = %s'
data = await db.query(sql, 'abc')
print('query():', data)
item = {
'name': 'abc',
'content': '123'
}
i = 0
while 1:
i += 1
if i % 2 == 0:
lastid = await db.table_insert('test', item, ignore_duplicated=False)
else:
lastid = await db.table_insert('test', item)
print('insert():', lastid)
await asyncio.sleep(1)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(test(loop))