QSql refresh()?



  • is there any function that will output my new query entry without exiting and running the program again to reload data from the database?





  • @the_ yes i follow what you said on that thread but is not refreshing when i click submit



  • @the_ i want to display the query when i clicked the button submit without rerunning the program



  • How do you display the data when you click the submit button?



  • @the_
    else:
    self.con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="incoming_mac")
    self.manager = self.con.cursor()
    self.insert_this_data = ("""INSERT INTO incoming_mac_records(DATE_RECEIVED,RECEIVED_BY,DELIVERED_BY,INVOICE_NUMBER) VALUES('%s','%s','%s',%s)"""%(DATE_RECEIVED,RECEIVED_BY,DELIVERED_BY,str(INVOICE)))
    self.manager.execute(self.insert_this_data)
    self.con.commit()
    self.con.close()
    print "Okay na bes!"

            self.projectModel = QSqlQueryModel()
            self.projectModel.setQuery("select * from incoming_mac_records",self.db)


  • @the_
    self.db = QSqlDatabase.addDatabase("QMYSQL")

        self.db.setHostName("localhost")
        self.db.setDatabaseName("incoming_mac")
        self.db.setUserName("root")
        self.db.setPassword("admingelo")
        self.db.open()
    
        self.projectModel = QSqlQueryModel()
        self.projectModel.setQuery("select * from incoming_mac_records",self.db)
    
        self.projectView = QTableView(self)
        self.projectView.setModel(self.projectModel)
        self.projectView.resizeColumnsToContents()
    
        self.projectView.setGeometry(250,10,940,680)


  • @the_ sorry for the long codes i am new to programming

    import sys
    import os
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4.QtSql import *
    from PyQt4 import QtGui,QtCore
    import mysql.connector

    class Window(QtGui.QMainWindow):
    def init(self):
    super(Window, self).init()
    self.setGeometry(50,50,1200,700)
    self.setWindowTitle("Inventory and Monitoring System")
    self.setWindowIcon(QtGui.QIcon('Pictures/delta.png'))
    QApplication.setStyle(QStyleFactory.create('Cleanlooks'))

        self.db = QSqlDatabase.addDatabase("QMYSQL")
    
        self.db.setHostName("localhost")
        self.db.setDatabaseName("incoming_mac")
        self.db.setUserName("root")
        self.db.setPassword("admingelo")
        self.db.open()
    
        self.projectModel = QSqlQueryModel()
        self.projectModel.setQuery("select * from incoming_mac_records",self.db)
    
        self.projectView = QTableView(self)
        self.projectView.setModel(self.projectModel)
        self.projectView.resizeColumnsToContents()
    
        self.projectView.setGeometry(250,10,940,680)
        
                
        self.buttons()
    
    def buttons(self):
        #Search function
        self.search = QLineEdit(self)
        self.search.resize(150,25)
        self.searchLabel = QLabel("Search",self)
        self.searchLabel.move(10,10)
        self.search.move(90,10)
        self.searchbtn = QtGui.QPushButton("SEARCH",self)
        self.searchbtn.clicked.connect(self.searchFunction)
        self.searchbtn.resize(self.searchbtn.minimumSizeHint())
        self.searchbtn.move(160,40)
        #End of search function
    
        #start input 1
        self.Date_received = QtGui.QLineEdit(self)
        self.Date_received.resize(150,25)
        self.InputLabel = QtGui.QLabel("Date received",self)
        self.Date_received.move(90,100)
        self.InputLabel.move(10,99)
        #end Input 1
    
        #start input 2
        self.query = QSqlQuery("SELECT name FROM names_delta")
        self.Received_by = QComboBox(self)
        self.Received_by.currentIndexChanged.connect(self.inputFunction)
        self.Received_by.resize(150,25)
        self.Input2Label = QLabel("Received by",self)
        self.Input2Label.move(10,130)
        self.Received_by.move(90,130)
        
        while(self.query.next()):
            self.Received_by.addItem(self.query.value(0).toString())
        self.DATA_COMBOBOX = self.Received_by.currentText()
        #end input 2
    
        #start input 3
        self.Delivered_by = QtGui.QLineEdit(self)
        self.Delivered_by.resize(150,25)
        self.Input3Label = QtGui.QLabel("Delivered by",self)
        self.Input3Label.move(10,160)
        self.Delivered_by.move(90,160)
        #end input 3
    
        #start input 4
        self.Invoice = QtGui.QLineEdit(self)
        self.Invoice.resize(150,25)
        self.Input4Label = QtGui.QLabel("Invoice #",self)
        self.Input4Label.move(10,190)
        self.Invoice.move(90,190)
        #end input 4
    
        #start input 5
        self.Purchase_order = QtGui.QLineEdit(self)
        self.Purchase_order.resize(150,25)
        self.Input5Label = QtGui.QLabel("Purchase Order",self)
        self.Input5Label.move(10,220)
        self.Purchase_order.move(90,220)
        #end input 5
    
        #start input 6
        self.Model = QtGui.QLineEdit(self)
        self.Model.resize(150,25)
        self.Input6Label = QtGui.QLabel("Model",self)
        self.Input6Label.move(10,250)
        self.Model.move(90,250)
        #end input 6
    
        #start input 7
        self.Specification = QtGui.QLineEdit(self)
        self.Specification.resize(150,25)
        self.Input7Label = QtGui.QLabel("Specification",self)
        self.Input7Label.move(10,280)
        self.Specification.move(90,280)
        #end input 7
    
        #start input 8
        self.Quantity = QtGui.QLineEdit(self)
        self.Quantity.resize(150,25)
        self.Input8Label = QtGui.QLabel("Quantity",self)
        self.Input8Label.move(10,310)
        self.Quantity.move(90,310)
        #end input 8
    
        #start input 9
        self.Serial = QtGui.QLineEdit(self)
        self.Serial.resize(150,25)
        self.Input9Label = QtGui.QLabel("Serial #",self)
        self.Input9Label.move(10,340)
        self.Serial.move(90,340)
        #end input 9
    
        #start input 10
        self.Sticker = QtGui.QLineEdit(self)
        self.Sticker.resize(150,25)
        self.Input10Label = QtGui.QLabel("Sticker #",self)
        self.Input10Label.move(10,370)
        self.Sticker.move(90,370)
        #end input 10
    
        #start input 11
        self.Asset = QtGui.QLineEdit(self)
        self.Asset.resize(150,25)
        self.Input11Label = QtGui.QLabel("Asset #",self)
        self.Input11Label.move(10,400)
        self.Asset.move(90,400)
        #end input 11
    
        #start input 12
        self.Warranty_Expiry = QtGui.QLineEdit(self)
        self.Warranty_Expiry.resize(150,25)
        self.Input12Label = QtGui.QLabel("Warranty Expiry",self)
        self.Input12Label.move(10,430)
        self.Warranty_Expiry.move(90,430)
        #end input 12
    
        #
        self.Issued_to = QtGui.QLineEdit(self)
        self.Issued_to.resize(150,25)
        self.Input16Label = QtGui.QLabel("Issued to",self)
        self.Input16Label.move(10,460)
        self.Issued_to.move(90,460)
    
        #
    
        #start input 13
        self.Site = QtGui.QLineEdit(self)
        self.Site.resize(150,25)
        self.Input13Label = QtGui.QLabel("Site",self)
        self.Input13Label.move(10,490)
        self.Site.move(90,490)
        #end input 13
    
        #start input 14
        self.Department = QtGui.QLineEdit(self)
        self.Department.resize(150,25)
        self.Input14Label = QtGui.QLabel("Department",self)
        self.Input14Label.move(10,520)
        self.Department.move(90,520)
        #end input 14
    
        #start input 15
        self.Amount = QtGui.QLineEdit(self)
        self.Amount.resize(150,25)
        self.Input15Label = QLabel("Amount",self)
        self.Input15Label.move(10,550)
        self.Amount.move(90,550)
        #end input 15
    
        #Input submit
        self.Submitbtn = QtGui.QPushButton("SUBMIT",self)
        self.Submitbtn.clicked.connect(self.inputFunction)
        self.Submitbtn.resize(self.Submitbtn.minimumSizeHint())
        self.Submitbtn.move(160,580)
        #Input end
        self.show()
    
    def searchFunction(self):
        SEARCH = self.search.text()
        if SEARCH == "":
            print "Enter something"
        else:
            print (SEARCH)
    
    def inputFunction(self):
        DATE_RECEIVED    = self.Date_received.text()
        RECEIVED_BY      = self.DATA_COMBOBOX
        DELIVERED_BY     = self.Delivered_by.text()
        INVOICE          = self.Invoice.text()
        PURCHASE_ORDER   = self.Purchase_order.text()
        MODEL            = self.Model.text()
        SPECIFICATION    = self.Specification.text()
        QUANTITY         = self.Quantity.text()
        SERIAL           = self.Serial.text()
        STICKER          = self.Sticker.text()
        ASSET            = self.Asset.text()
        WARRANTY_EXPIRY  = self.Warranty_Expiry.text()
        ISSUED_TO        = self.Issued_to.text()
        SITE             = self.Site.text()
        DEPARTMENT       = self.Department.text()
        AMOUNT           = self.Amount.text()
    
        if DATE_RECEIVED == "":
            print "full up all form"
        else:
            self.con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="incoming_mac")
            self.manager = self.con.cursor()
            self.insert_this_data = ("""INSERT INTO incoming_mac_records(DATE_RECEIVED,RECEIVED_BY,DELIVERED_BY,INVOICE_NUMBER) VALUES('%s','%s','%s',%s)"""%(DATE_RECEIVED,RECEIVED_BY,DELIVERED_BY,str(INVOICE)))
            self.manager.execute(self.insert_this_data)
            self.con.commit()
            self.con.close()
            print "Okay na bes!"
    
            self.projectModel = QSqlQueryModel()
            self.projectModel.setQuery("select * from incoming_mac_records",self.db)
    

    def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())
    run()



  • @the_ please help me



  • As far as I can see you are asking this question again and again and again. If you use the search function in this forum you could find an answer

    here for example (you did post in there too)

    https://forum.qt.io/topic/1168/solved-the-best-way-to-programmatically-refresh-a-qsqlquerymodel-when-the-content-of-the-query-changes/14


  • Qt Champions 2016

    Please don't post the same question multiple times! It's shows disrespect to the people trying to help you on their own time. Stick to your original thread and provide more information, or ask additional related questions there.
    Consider yourself warned.



  • @the_ how to use this code?

    '''
    QString queryStr = model->query().executedQuery();
    model->clear();
    model->query().clear();
    model->setQuery(queryStr);

    with this code? because this is the way i refresh a table is this bad?

    import sys
    import os
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4.QtSql import *
    from PyQt4 import QtGui,QtCore
    import mysql.connector

    class Window(QtGui.QMainWindow):
    def init(self):
    super(Window, self).init()
    self.setGeometry(50,50,500,300)
    self.setWindowTitle("Inventory and Monitoring System")
    self.setWindowIcon(QtGui.QIcon('Pictures/delta.png'))
    QApplication.setStyle(QStyleFactory.create('Cleanlooks'))

        self.db = QSqlDatabase.addDatabase("QMYSQL")
    
        self.db.setHostName("localhost")
        self.db.setDatabaseName("incoming_mac")
        self.db.setUserName("root")
        self.db.setPassword("admingelo")
        self.db.open()
    
        self.Date_received = QLineEdit(self)
        self.searchbtn = QtGui.QPushButton("SUBMIT",self)
        self.searchbtn.clicked.connect(self.Database)
        self.searchbtn.move(10,50)
        self.searchbtn1 = QtGui.QPushButton("REFRESH",self)
        self.searchbtn1.clicked.connect(self.RefreshTable)
        self.searchbtn1.move(10,90)
    
        self.show()
       
    
    def Database(self):
        DATE_RECEIVED = self.Date_received.text()
        self.con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="incoming_mac")
        self.manager = self.con.cursor()
        self.insert_this_data = ("""INSERT INTO incoming_mac_records(DATE_RECEIVED) VALUES('%s')"""%str(DATE_RECEIVED))
        self.manager.execute(self.insert_this_data)
        self.con.commit()
        self.con.close()
    
    def RefreshTable(self):
        self.projectModel = QSqlQueryModel()
        self.projectModel.setQuery("select * from incoming_mac_records",self.db)
    
        self.projectView = QTableView(self)
        self.projectView.setModel(self.projectModel)
        self.projectView.resizeColumnsToContents()
    
        self.projectView.setGeometry(50,50,300,200)
    
     
        self.projectView.show()
    

    def run():
    app = QtGui.QApplication(sys.argv)
    GUI = Window()
    sys.exit(app.exec_())
    run()



  • @the_ IS THIS CODE OKAY? IF I WANT TO REFRESH A TABLE EVERY 3 OR MORE SECONDS?

    import sys
    import time
    import os

    x = 0
    while x != 3:
    x+=1
    print (x)
    #
    '''
    AND THE FUNCTION HERE TO REFRESH EVERY 3 OR MORE SECONDS?
    '''
    #
    time.sleep(2)
    if x >= 3:
    x = 0



  • Take a look at QTimer



  • @the_ how to translate this in python?

    QString queryStr = model->query().executedQuery();
    model->clear();
    model->query().clear();
    model->setQuery(queryStr);

    and what is model?



  • This thread must be a joke :/



  • @BjornW for you it is. Maybe your an expert in coding that's why you are so boastful i am asking because i want to understand what i ask not to tell a joke

    don't you worry next time i will try to ask "How to handle Overconfident people"



  • Model is the QSqlQueryModel of your QTableView


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.