added update_filed function
authorMax Usachev <maxusachev@gmail.com>
Tue, 8 Jun 2010 12:43:00 +0000 (15:43 +0300)
committerMax Usachev <maxusachev@gmail.com>
Tue, 8 Jun 2010 12:43:00 +0000 (15:43 +0300)
database/SQLite.py

index 6d67aa4..656a331 100644 (file)
@@ -19,7 +19,7 @@ class SQLite:
         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 field (id int primary key, name str)""")
         self.conn.execute("""CREATE TABLE relation (data_id int, \
             struct_id int)""")
         self.conn.execute("""CREATE TABLE struct (id int, name str, \
@@ -32,9 +32,18 @@ class SQLite:
         self.conn.commit()
         self.conn.close()
 
+    def update_field(self, fname):
+        """Adds new field to database."""
+
+        fields = self.conn.execute("""SELECT name from field""").fetchall()
+        if not fname in fields:
+            self.conn.execute("""INSERT INTO field values((SELECT max(id) 
+            FROM field)+1, ?)""", (fname,))
+
 
 if __name__ == "__main__":
     db = SQLite('/tmp/')
+    db.update_field('test')
     db.close()