<div class="gmail_quote">[sorry for the posting more messages, but I figured out now, that my subscription address was wrong, and I got no error message from the list, that my messages will not appear :(]<br><br>Hi!<br>
<br>
I am a novice pyqt developer.<br>
I will develop a pyqt program, which will periodically will get<br>
measurement data, and display it visually. That's why I have chosen to<br>
store it in sqlite database. Measurements have some metadata, so I<br>
thought they should also go to the same db, in an ini like table<br>
(table name preferences, key and value are strings).<br>
<br>
I have written 2 classes for that, but that does not seems like a<br>
straighforward solution... I hope there is a cleaner and more<br>
efficient way...<br>
<br>
class BatteryDb(QSqlDatabase):<br>
@classmethod<br>
def addSqlDb(cls, filename=None):<br>
db = QSqlDatabase.addDatabase("QSQLITE", 'battery')<br>
# if filename is None FIXME<br>
db.setDatabaseName(filename)<br>
db.open()<br>
return db<br>
<br>
@classmethod<br>
def removeSqlDb(cls, db):<br>
print("battery db delete")<br>
db.close()<br>
QSqlDatabase.removeDatabase('battery')<br>
<br>
class IniTable:<br>
def __init__(self, db, table, parent=None):<br>
self.model = QSqlTableModel(parent, db)<br>
self.model.setTable(table)<br>
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)<br>
self.db = db<br>
self.model.select()<br>
self.build_indices()<br>
<br>
def build_indices(self):<br>
self.indices = {}<br>
for i in range(self.model.rowCount()):<br>
self.indices[self.model.record(i).value('key')] = i<br>
<br>
def __getitem__(self, attr):<br>
return self.model.record(self.indices[attr]).value('value')<br>
<br>
def __setitem__(self, attr, value):<br>
if attr in self.indices:<br>
idx = self.indices[attr]<br>
record = self.model.record(idx)<br>
record.setValue('value', value)<br>
self.model.setRecord(idx, record)<br>
# idx = self.model.createIndex(self.indices[attr],<br>
self.model.fieldIndex('value'))<br>
#self.model.setData(idx, value)<br>
else:<br>
record = self.model.record()<br>
record.setValue('key', attr)<br>
record.setValue('value', value)<br>
res = self.model.insertRecord(-1, record)<br>
#self.build_indices()<br>
print('.')<br>
<br>
def submitAll(self):<br>
self.model.submitAll()<br>
<font color="#888888"><br>
+-[ Gergely Kontra <<a href="mailto:pihentagy@gmail.com">pihentagy@gmail.com</a>> ]------------------+<br>
| |<br>
| Mobile:(+36 20)356 9656 |<br>
| |<br>
+- "Olyan lángész vagyok, hogy poroltóval kellene járnom!" -+<br>
</font></div><br>