I'm trying to use a Python dictionary to create a SQLite record as follows, but sometimes one of the keys might not exist, which causes an error, so I've tried to make sure all keys always exist and will be populated as NULL using None, but this also doesn't work ('NoneType' object is not iterable). What am I missing?
def insert_device(self, device):
try:
db = sqlite3.connect(self.db_path)
cursor = db.cursor()
for column in cursor.description:
print(column)
if column not in device:
device[column] = None
cursor.execute('''INSERT INTO devices(created, name, location_id, model, port) VALUES(CURRENT_TIMESTAMP,?,?,?,?)''', [device['name'], device['location_id'], device['model'], device['port']])
cursor.close()
db.commit()
db.close()
except Exception as e:
self._logger.error("Error inserting device into database: %s" % e)