def insert(self): """ Inserts a new entry into the table, and assigns the "auto incremented" ID. It's probably a dangerous assumption that the PrimaryField is using that, so I may need to change this in the future. """ sql = u'INSERT INTO %s' % self.table() keys = [] values = [] format_values = [] for field in self.fields(): attr = object.__getattribute__(self, field) if attr.auto_value: continue keys.append(field) format_values.append(attr.format) values.append(attr._value) keys_str = u'( %s )' % u', '.join(keys) values_str = u'VALUES( %s )' % u', '.join(format_values) sql = '%s %s %s;' % (sql, keys_str, values_str) connection.execute(sql, values) primary_k = self.__class__.get_primary() primary = object.__getattribute__(self, primary_k) primary.value = connection.connection.insert_id()
def drop_table(cls): """ Generates and executes the SQL to DROP a table. """ if not connection.connected: raise Exception('Not connected to the database.') sql = u'DROP TABLE IF EXISTS %s' % cls.table() connection.execute(sql)
def create_table(cls): """ Generates and executes the SQL to create a table. """ if not connection.connected: raise Exception('Not connected to the database.') cursor = connection.execute(cls.create_table_sql()) cursor.close()
def __iter__(self): """ Gets the SQL and makes the call, then stores the result on the class for the __next__() call(s). """ if not connection.connected: raise Exception('Not connected to the database.') if not self.cursor: sql = self.get_sql() self.cursor = connection.execute(sql, tuple(self.values)) return self