AQuickTutorial » History » Version 6
Aurynn Shaw, 01/22/2009 09:09 AM
1 | 1 | Aurynn Shaw | = Simpycity in a Hurry = |
---|---|---|---|
2 | And what to expect when you use it. |
||
3 | |||
4 | == Setup == |
||
5 | |||
6 | First, set up Simpycity: |
||
7 | |||
8 | {{{ |
||
9 | 2 | Aurynn Shaw | from simpycity.core import Function |
10 | 1 | Aurynn Shaw | from simpycity import config |
11 | |||
12 | config.host = 'localhost' |
||
13 | config.port = 5432 |
||
14 | 2 | Aurynn Shaw | config.user = 'user' |
15 | config.password = 'password' |
||
16 | config.database = 'dbname' |
||
17 | |||
18 | }}} |
||
19 | |||
20 | Then, create some basic functions. |
||
21 | {{{ |
||
22 | 6 | Aurynn Shaw | >>> f = Function("get_row",['id']) |
23 | >>> f_all = Function("get_rows") |
||
24 | 2 | Aurynn Shaw | }}} |
25 | |||
26 | 3 | Aurynn Shaw | '''f_all''' maps to the stored procedure "get_rows", which takes no arguments. |
27 | 2 | Aurynn Shaw | |
28 | 3 | Aurynn Shaw | '''f''', on the other hand, maps to the stored procedure "get_row", which takes a single argument, which we've named id. |
29 | 2 | Aurynn Shaw | |
30 | 3 | Aurynn Shaw | To call f_all, it's a standard python function: |
31 | 1 | Aurynn Shaw | {{{ |
32 | 6 | Aurynn Shaw | >>> all_results = f_all() |
33 | 3 | Aurynn Shaw | }}} |
34 | |||
35 | For get_row, it's the same Python call semantics, both positional and keyword arguments being supported |
||
36 | {{{ |
||
37 | 6 | Aurynn Shaw | >>> result = f(1) # get id 1 |
38 | >>> result = f(id=1) # also get id 1 |
||
39 | 1 | Aurynn Shaw | }}} |
40 | 4 | Aurynn Shaw | |
41 | The results (look kind of like) a result set from psycopg2, and can be iterated over as per normal: |
||
42 | {{{ |
||
43 | 6 | Aurynn Shaw | >>> for row in all_result: |
44 | ... # do row stuff |
||
45 | 4 | Aurynn Shaw | }}} |
46 | |||
47 | and in the case of a single result, you can also call .next() or .fetchone() and get the row object. |
||
48 | {{{ |
||
49 | 6 | Aurynn Shaw | >>> row = result.next() |
50 | 4 | Aurynn Shaw | # or |
51 | 6 | Aurynn Shaw | >>> row = result.fetchone() |
52 | 4 | Aurynn Shaw | }}} |