简体   繁体   English

使用QlineEdit在PyQt中更新SQLite数据库

[英]Use QlineEdit to update SQLite database in PyQt

I am trying to get multiple QLineEdit objects to update on clicking a submit pushbutton to the SQLite db. 我试图获取多个QLineEdit对象,以在单击向SQLite数据库的提交按钮时进行更新。 I also have a QTableView that displays the db. 我也有一个QTableView显示数据库。 The submit button adds a row to the database but does not save it. 提交按钮将一行添加到数据库,但不保存。 I would like the line edits to be added to the newly created row. 我希望将行编辑添加到新创建的行中。

import sys
from testdbtableform import *
from PyQt4 import *
from PyQt4 import QtSql, QtGui, QtCore
from PyQt4.QtSql import (QSqlDatabase, QSqlQuery, QSqlRelation,
        QSqlRelationalDelegate, QSqlRelationalTableModel)


def createConnection():
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('demomap.db')
    db.open()
    print (db.lastError().text())
    return True

class MyForm(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable("userlist")
        self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
        self.model.select()
        self.ui.tableView.setModel(self.model)
        QtCore.QObject.connect(self.ui.Submit, QtCore.SIGNAL('clicked()'), self.dbinput)

    def dbinput(self):
        row = self.model.rowCount()
        self.model.insertRow(row)
        self.model.submitAll()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

I have 2 line edits to input to columns of username and email. 我有2行编辑输入到用户名和电子邮件列。 If I could get an example of how to use it correctly I would be able to understand the rest of the inputs. 如果我能得到一个如何正确使用它的例子,我将能够理解其余的输入。 I am fairly new to python and pyqt. 我对python和pyqt相当陌生。 If I must switch it to pyside I am willing to do so. 如果必须将其切换为pyside,我愿意这样做。

You should commit data to the database. 您应该将数据提交到数据库。 Using OnManualSubmit caches the data into the model. 使用OnManualSubmit将数据缓存到模型中。

self.model.submitAll() maybe? self.model.submitAll()也许?

import sys
from testdbtableform import *
from PyQt4 import QtSql, QtGui, QtCore, QtSql

def createConnection():
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('demomap.db')
    if db.open():
        return True
    else:
        print db.lastError().text()
        return False

class MyForm(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable("userlist")
        self.model.setEditStrategy(2)
        self.model.select()
        self.ui.tableView.setModel(self.model)
        self.ui.Submit.clicked.connect(self.dbinput)

    def dbinput(self):
        self.model.insertRow(-1)
        text = self.ui.lineEdit.text()
        if self.model.setData(self.model.index(-1, 0), text):
            self.model.submitAll()
        else:
            print "There was a problem setting the data."

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

I changed dbinput to show you how it should work and I also cleaned up a few other bits of your code that needed help. 我更改了dbinput以向您展示它如何工作,并且还清理了代码中需要帮助的其他部分。

This should work for the most part but I can't test it without you db file and your ui file. 这在大多数情况下应该可以工作,但是如果没有db文件和ui文件,我将无法对其进行测试。

It looks like you need to spend a little more time reading and understanding the documentation. 看来您需要花费更多时间阅读和理解文档。 http://pyqt.sourceforge.net/Docs/PyQt4/classes.html http://pyqt.sourceforge.net/Docs/PyQt4/classes.html

Good luck with it. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM