[PyQt] QSqlTableModel without view
KONTRA, Gergely
pihentagy at gmail.com
Fri Dec 3 14:50:28 GMT 2010
[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 :(]
Hi!
I am a novice pyqt developer.
I will develop a pyqt program, which will periodically will get
measurement data, and display it visually. That's why I have chosen to
store it in sqlite database. Measurements have some metadata, so I
thought they should also go to the same db, in an ini like table
(table name preferences, key and value are strings).
I have written 2 classes for that, but that does not seems like a
straighforward solution... I hope there is a cleaner and more
efficient way...
class BatteryDb(QSqlDatabase):
@classmethod
def addSqlDb(cls, filename=None):
db = QSqlDatabase.addDatabase("QSQLITE", 'battery')
# if filename is None FIXME
db.setDatabaseName(filename)
db.open()
return db
@classmethod
def removeSqlDb(cls, db):
print("battery db delete")
db.close()
QSqlDatabase.removeDatabase('battery')
class IniTable:
def __init__(self, db, table, parent=None):
self.model = QSqlTableModel(parent, db)
self.model.setTable(table)
self.model.setEditStrategy(QSqlTableModel.OnManualSubmit)
self.db = db
self.model.select()
self.build_indices()
def build_indices(self):
self.indices = {}
for i in range(self.model.rowCount()):
self.indices[self.model.record(i).value('key')] = i
def __getitem__(self, attr):
return self.model.record(self.indices[attr]).value('value')
def __setitem__(self, attr, value):
if attr in self.indices:
idx = self.indices[attr]
record = self.model.record(idx)
record.setValue('value', value)
self.model.setRecord(idx, record)
# idx = self.model.createIndex(self.indices[attr],
self.model.fieldIndex('value'))
#self.model.setData(idx, value)
else:
record = self.model.record()
record.setValue('key', attr)
record.setValue('value', value)
res = self.model.insertRecord(-1, record)
#self.build_indices()
print('.')
def submitAll(self):
self.model.submitAll()
+-[ Gergely Kontra <pihentagy at gmail.com> ]------------------+
| |
| Mobile:(+36 20)356 9656 |
| |
+- "Olyan lángész vagyok, hogy poroltóval kellene járnom!" -+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20101203/9984837b/attachment.html>
More information about the PyQt
mailing list