annotate frontend/mfrontend/db.py @ 1:c3fb1e9fc1f7

(none)
author michalr
date Tue, 22 Feb 2011 18:10:18 +0000
parents
children cf786ee26a22
rev   line source
1
michalr
parents:
diff changeset
1 #!/usr/bin/python
michalr
parents:
diff changeset
2 # -*- coding: utf-8 -*-
michalr
parents:
diff changeset
3
michalr
parents:
diff changeset
4 import sqlite3
michalr
parents:
diff changeset
5 from flask import g
michalr
parents:
diff changeset
6 import ConfigParser
michalr
parents:
diff changeset
7
michalr
parents:
diff changeset
8 config = ConfigParser.SafeConfigParser()
michalr
parents:
diff changeset
9 config.read('config.ini')
michalr
parents:
diff changeset
10
michalr
parents:
diff changeset
11 def connect_db():
michalr
parents:
diff changeset
12 """Funkcja łącząca się z bazą danych."""
michalr
parents:
diff changeset
13 g.db = sqlite3.connect(config.get('Basic','Database'))
michalr
parents:
diff changeset
14
michalr
parents:
diff changeset
15 def disconnect_db():
michalr
parents:
diff changeset
16 """Funkcja zamykająca połączenie z bazą danych"""
michalr
parents:
diff changeset
17 g.db.close()
michalr
parents:
diff changeset
18
michalr
parents:
diff changeset
19 def query_db(query, args=(), one=False):
michalr
parents:
diff changeset
20 """Funkcja ułatwiająca odpytywanie bazy danych, źródło:
michalr
parents:
diff changeset
21 http://flask.pocoo.org/docs/patterns/sqlite3/
michalr
parents:
diff changeset
22 """
michalr
parents:
diff changeset
23 cur = g.db.execute(query, args)
michalr
parents:
diff changeset
24 rv = [dict((cur.description[idx][0], value)
michalr
parents:
diff changeset
25 for idx, value in enumerate(row)) for row in cur.fetchall()]
michalr
parents:
diff changeset
26 return (rv[0] if rv else None) if one else rv