added new SQlite database
authorMax Usachev <maxusachev@gmail.com>
Tue, 8 Jun 2010 12:05:05 +0000 (15:05 +0300)
committerMax Usachev <maxusachev@gmail.com>
Tue, 8 Jun 2010 12:05:05 +0000 (15:05 +0300)
database/SQLite.py [new file with mode: 0644]

diff --git a/database/SQLite.py b/database/SQLite.py
new file mode 100644 (file)
index 0000000..6d67aa4
--- /dev/null
@@ -0,0 +1,40 @@
+import os
+import sqlite3
+
+DATABASE_NAME = 'contacts.db'
+
+
+class SQLite:
+    def __init__(self, basedir):
+        self._path = os.path.join(basedir, DATABASE_NAME)
+        self.conn = None
+        if not os.path.exists(self._path):
+            self.new()
+        else:
+            self.conn = sqlite3.connect(self._path)
+
+    def new(self):
+        """Creates new databse."""
+
+        self.conn = sqlite3.connect(self._path)
+        self.conn.execute("""CREATE TABLE data (user_id int, field_id int, \
+            value str)""")
+        self.conn.execute("""CREATE TABLE field (field_id int, name str)""")
+        self.conn.execute("""CREATE TABLE relation (data_id int, \
+            struct_id int)""")
+        self.conn.execute("""CREATE TABLE struct (id int, name str, \
+            parent_id int)""")
+        self.conn.commit()
+
+    def close(self):
+        """Closes connection with database."""
+
+        self.conn.commit()
+        self.conn.close()
+
+
+if __name__ == "__main__":
+    db = SQLite('/tmp/')
+    db.close()
+
+