Initial commit.
[bloggy.git] / common.py
blobeb08964c9c11fb04fcb6561178c8179a55f0f375
1 import sqlite3
2 import sys
4 DBFILENAME = "bloggy.db"
6 def connect():
7 try:
8 conn = sqlite3.connect(DBFILENAME)
9 conn.execute("CREATE TABLE IF NOT EXISTS entries (id INTEGER PRIMARY KEY, date , title TEXT, text TEXT)")
10 except sqlite3.OperationalError:
11 print 'Failed to connect to database. Check file and directory permissions.'
12 sys.exit(1)
13 return conn
15 def getnumposts(conn, id=None):
16 if id:
17 numposts = conn.execute("SELECT count(id) FROM entries WHERE id = ?", (id,)).fetchone()
18 else:
19 numposts = conn.execute("SELECT count(id) FROM entries").fetchone()
20 return int(numposts[0])