32
32
Use :py:meth:`~postgres.Postgres.all` to run SQL and fetch all results:
33
33
34
34
>>> db.all("SELECT * FROM foo ORDER BY bar")
35
- [Record(bar=u 'bit', baz=537), Record(bar=u 'buz', baz=42)]
35
+ [Record(bar='bit', baz=537), Record(bar='buz', baz=42)]
36
36
37
37
Use :py:meth:`~postgres.Postgres.one_or_zero` to run SQL and fetch one result
38
38
or :py:class:`None`:
39
39
40
40
>>> db.one_or_zero("SELECT * FROM foo WHERE bar='buz'")
41
- Record(bar=u 'buz', baz=42)
41
+ Record(bar='buz', baz=42)
42
42
>>> db.one_or_zero("SELECT * FROM foo WHERE bar='blam'")
43
43
44
- If your query returns one column then you get a list of values instead of
45
- Records:
44
+ If your queries return one column then you get just the value or a list of
45
+ values instead of a Record or list of Records:
46
46
47
+ >>> db.one_or_zero("SELECT baz FROM foo WHERE bar='buz'")
48
+ 42
47
49
>>> db.all("SELECT baz FROM foo ORDER BY bar")
48
50
[537, 42]
49
51
59
61
formatting, but it is not the same.)
60
62
61
63
>>> db.one_or_zero("SELECT * FROM foo WHERE bar=%(bar)s", {"bar": "buz"})
62
- Record(bar=u 'buz', baz=42)
64
+ Record(bar='buz', baz=42)
63
65
64
66
Never build SQL strings out of user input!
65
67
89
91
... txn.execute("SELECT * FROM foo ORDER BY bar")
90
92
... txn.fetchall()
91
93
...
92
- [Record(bar=u 'bit', baz=537), Record(bar=u 'blam', baz=None), Record(bar=u 'buz', baz=42)]
94
+ [Record(bar='bit', baz=537), Record(bar='blam', baz=None), Record(bar='buz', baz=42)]
93
95
94
96
Note that other calls won't see the changes on your transaction until the end
95
97
of your code block, when the context manager commits the transaction for you::
99
101
... txn.execute("INSERT INTO foo VALUES ('blam')")
100
102
... db.all("SELECT * FROM foo ORDER BY bar")
101
103
...
102
- [Record(bar=u 'bit', baz=537), Record(bar=u 'buz', baz=42)]
104
+ [Record(bar='bit', baz=537), Record(bar='buz', baz=42)]
103
105
>>> db.all("SELECT * FROM foo ORDER BY bar")
104
- [Record(bar=u 'bit', baz=537), Record(bar=u 'blam', baz=None), Record(bar=u 'buz', baz=42)]
106
+ [Record(bar='bit', baz=537), Record(bar='blam', baz=None), Record(bar='buz', baz=42)]
105
107
106
108
The :py:func:`~postgres.Postgres.get_transaction` manager gives you a cursor
107
109
with :py:attr:`autocommit` turned off on its connection. If the block under
117
119
... cursor.execute("SELECT * FROM foo ORDER BY bar")
118
120
... cursor.fetchall()
119
121
...
120
- [Record(bar=u 'bit', baz=537), Record(bar=u 'buz', baz=42)]
122
+ [Record(bar='bit', baz=537), Record(bar='buz', baz=42)]
121
123
122
124
A connection gotten in this way will have :py:attr:`autocommit` turned off, and
123
125
it'll never be implicitly committed otherwise. It'll actually be rolled back
@@ -342,13 +344,13 @@ def all(self, sql, parameters=None):
342
344
:returns: :py:class:`list` of records or single values
343
345
344
346
>>> db.all("SELECT * FROM foo ORDER BY bar")
345
- [Record(bar=u 'bit', baz=537), Record(bar=u 'buz', baz=42)]
347
+ [Record(bar='bit', baz=537), Record(bar='buz', baz=42)]
346
348
347
349
If the query results in a single column, we return a list of values
348
350
rather than a list of records of values.
349
351
350
- >>> db.all("SELECT bar FROM foo ORDER BY bar")
351
- ['bit', 'buz' ]
352
+ >>> db.all("SELECT baz FROM foo ORDER BY bar")
353
+ [537, 42 ]
352
354
353
355
"""
354
356
with self .get_transaction () as txn :
@@ -373,7 +375,7 @@ def one_or_zero(self, sql, parameters=None, zero=None):
373
375
it may not exist yet.
374
376
375
377
>>> db.one_or_zero("SELECT * FROM foo WHERE bar='buz'")
376
- Record(bar=u 'buz', baz=42)
378
+ Record(bar='buz', baz=42)
377
379
>>> row = db.one_or_zero("SELECT * FROM foo WHERE bar='blam'")
378
380
>>> if row is None:
379
381
... print("No blam yet.")
@@ -430,7 +432,7 @@ def get_transaction(self, *a, **kw):
430
432
... txn.execute("SELECT * FROM foo")
431
433
... txn.fetchall()
432
434
...
433
- [Record(bar=u 'buz', baz=42), Record(bar=u 'bit', baz=537)]
435
+ [Record(bar='buz', baz=42), Record(bar='bit', baz=537)]
434
436
435
437
"""
436
438
return TransactionContextManager (self .pool , * a , ** kw )
@@ -448,7 +450,7 @@ def get_connection(self):
448
450
... cursor.execute("SELECT * FROM foo")
449
451
... cursor.fetchall()
450
452
...
451
- [Record(bar=u 'buz', baz=42), Record(bar=u 'bit', baz=537)]
453
+ [Record(bar='buz', baz=42), Record(bar='bit', baz=537)]
452
454
453
455
"""
454
456
return ConnectionContextManager (self .pool )
0 commit comments