6d67aa4003f9b17978e2992d0de839515c79d78d
[meabook] / database / SQLite.py
1 import os
2 import sqlite3
3
4 DATABASE_NAME = 'contacts.db'
5
6
7 class SQLite:
8     def __init__(self, basedir):
9         self._path = os.path.join(basedir, DATABASE_NAME)
10         self.conn = None
11         if not os.path.exists(self._path):
12             self.new()
13         else:
14             self.conn = sqlite3.connect(self._path)
15
16     def new(self):
17         """Creates new databse."""
18
19         self.conn = sqlite3.connect(self._path)
20         self.conn.execute("""CREATE TABLE data (user_id int, field_id int, \
21             value str)""")
22         self.conn.execute("""CREATE TABLE field (field_id int, name str)""")
23         self.conn.execute("""CREATE TABLE relation (data_id int, \
24             struct_id int)""")
25         self.conn.execute("""CREATE TABLE struct (id int, name str, \
26             parent_id int)""")
27         self.conn.commit()
28
29     def close(self):
30         """Closes connection with database."""
31
32         self.conn.commit()
33         self.conn.close()
34
35
36 if __name__ == "__main__":
37     db = SQLite('/tmp/')
38     db.close()
39
40