[PySide, Python 3] QSqlQuery::value: not positioned on a valid record
-
Hello,
I'm trying to learn about model/view with Sql and widget mapping, and as I experiment I'm coming across some obstacles -- I assume these problems are pretty easy to diagnose for someone who has more experience:
I'm trying to map a lineedit and a spinbox to a text field & integer field in my sqlite database. The lineedit displays the data properly but it does not seem to submit my changes. I get the error: QSqlQuery::value: not positioned on a valid record.
Other problems:
- The spinbox does not seem to reflect the model data
- My "next" button only seems to go through 2 of the 4 data rows
- I have a hangnail but that's another issue entirely
Any suggestions on what I'm doing wrong would be much appreciated. See full code below. (In my actual script, the QTableview is not actually present, I just have it here to help me visualize the table as I experiment)
Thanks for any advice!
@
from PySide.QtGui import *
from PySide.QtCore import *
from PySide.QtSql import *
import sys
import sqlite3 as liteclass MainWindow(QMainWindow):
def init(self):
super(MainWindow, self).init()
self.c = CentralWidget()
self.setCentralWidget(self.c)
self.setWindowTitle("Mapping Widgets?")
self.resize(300,250)
self.show()class CentralWidget(QWidget):
def init(self):
super(CentralWidget, self).init()
self.grid = QGridLayout()
self.next = QPushButton("Next")self.text = QLineEdit() self.spin = QSpinBox() #self.text.textChanged.connect(self.submit) self.setupModel() self.grid.addWidget(self.next, 0,0) self.grid.addWidget(self.text, 1,0) self.grid.addWidget(self.spin, 1,1) self.setLayout(self.grid) def setupModel(self): global dbfile db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(dbfile) ok = db.open() if not ok: print("Invalid Database") return self.model = QSqlTableModel() self.model.setTable("TestModel") self.model.setEditStrategy(QSqlTableModel.OnFieldChange) self.model.removeColumn(0) self.model.select() self.view = QTableView() self.view.setModel(self.model) self.grid.addWidget(self.view, 2,0,2,2) self.mapper = QDataWidgetMapper() self.mapper.setModel(self.model) self.mapper.addMapping(self.text, 0) self.mapper.addMapping(self.spin, 1) self.mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit) self.mapper.toFirst() self.next.clicked.connect(self.mapper.toNext) # def submit(self): # print("text changed") # self.mapper.submit()
def createAndPopulate():
global dbfile
#con, cur = opendb(r'./data/testingPyQTSQLModel.sqlite')
con, cur = opendb(dbfile)ok = cur.execute("DROP TABLE IF EXISTS TestModel") print(ok) ok = cur.execute("CREATE TABLE TestModel \ (ID INTEGER PRIMARY KEY AUTOINCREMENT,\ Text1 VARCHAR(255),\ Int1 INTEGER)") ok = cur.execute("INSERT INTO TestModel VALUES( \ null, 'ABC', 235)") cur.execute("INSERT INTO TestModel VALUES( \ null, 'CAT', 13)") cur.execute("INSERT INTO TestModel VALUES( \ null, 'BLUE', 62)") cur.execute("INSERT INTO TestModel VALUES( \ null, 'FLOPPITY', 34)") con.commit() closedb(con)
def opendb(dbfile):
con = lite.connect(dbfile)
with con:
cur = con.cursor()
return con, curdef closedb(con):
con.close()def main():
global dbfiledbfile = 'test.sqlite' createAndPopulate() app = QApplication(sys.argv) win = MainWindow() sys.exit(app.exec_())
if name == 'main':
main()
@ -