Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can i pass my QLineEdit data into my database?



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

    class Window(QtGui.QMainWindow):
    def init(self):
    super(Window, self).init()
    self.setGeometry(50,50,0,0)
    self.setFixedSize(900,600)
    self.setWindowTitle("IMS Delta EarthMoving")
    self.setWindowIcon(QtGui.QIcon('delta.png'))
    pic = QtGui.QLabel(self)
    pic.setGeometry(240, -165, 698, 500)
    pic.setPixmap(QtGui.QPixmap(os.getcwd() + "/delta2.png"))

        self.Input()
        self.search()
        self.button()
        self.database()
             
    def search(self):
        assetSearch = QLabel("Asset search",self)
        assetSearch.move(10,10)
        AssetTag=QLineEdit(self)
        fbox=QFormLayout() 
        fbox.addRow(AssetTag)
        AssetTag.move(75,15)#left-right and up-down
        AssetTag.resize(150,20)
    
    def Input(self):
        INPUT1LABEL = QLabel("INPUT1",self)
        INPUT1LABEL.move(10,225)
        INPUT1 = QLineEdit(self)
        fbox=QFormLayout()
        fbox.addRow(INPUT1)
        INPUT1.move(75,230)
        INPUT1.resize(150,20)
      
        btnSubmit = QtGui.QPushButton("Submit",self)
        btnSubmit.clicked.connect(self.submit_function)
        btnSubmit.resize(btnSubmit.minimumSizeHint())
        btnSubmit.move(150,470)
        
    def button(self):
        btnQuit = QtGui.QPushButton("Quit", self)
        btnQuit.clicked.connect(self.close_application)
        btnQuit.resize(btnQuit.minimumSizeHint())
        btnQuit.move(150,138) #.move(left-right,top-bottom()
    
        btnSearch = QtGui.QPushButton("Search", self)
        #btnSearch.clicked.connect(self.) #add function for this ButtonBox
        btnSearch.resize(btnSearch.minimumSizeHint())
        btnSearch.move(150,40)
    
        btnUpdate = QtGui.QPushButton("Update",self)
        btnUpdate.clicked.connect(self.update_confirmation)
        btnUpdate.resize(btnUpdate.minimumSizeHint())
        btnUpdate.move(150,90)
    
        btnDelete = QtGui.QPushButton("Delete",self)
        #btnDelete.clicked.connect(self.) add function for this ButtonBox
        btnDelete.resize(btnDelete.minimumSizeHint())
        btnDelete.move(150,114)
    
        self.show()
    
    def close_application(self):
        choice = QtGui.QMessageBox.question(self, 'Exit',"Are you sure?",QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            sys.exit()
        else:
            pass
    
    def update_confirmation(self):
        choice = QtGui.QMessageBox.question(self,'Notice!',"Are you sure?",QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if choice == QtGui.QMessageBox.Yes:
            #put the function here!
            choice = QtGui.QMessageBox.question(self,'IMS',"Query saved",QtGui.QMessageBox.Ok)
            if choice == QtGui.QMessageBox.Ok:
                pass
        else:
            pass
     
    def submit_function(self):
        con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="test")
        manager = con.cursor()
        
        Date = 'Ai'
        Name = 'g'
        Sex  = 'g'
        
        insert_this_data = ("INSERT INTO test_table(name,age,sex) VALUES(%s,%s,%s)")
    
        data = (Date,Name,Sex)
    
        manager.execute(insert_this_data,data)
    
        con.commit()
        print "Insert Successful!"
    
    def database(self): #Table data view
        table 	= QTableWidget(self)
        db 	= QSqlDatabase.addDatabase("QMYSQL")
    
        db.setHostName("localhost")
        db.setDatabaseName("test")
        db.setUserName("root")
        db.setPassword("admingelo")
    
        if (db.open()==False):     
            QMessageBox.critical(None, "Database Error",
    		    db.lastError().text())   
    		    
        query = QSqlQuery ("SELECT * FROM sfo")   
    
        table.setColumnCount(query.record().count())
        table.setRowCount(query.size())
    
        index=0
        while (query.next()):
                table.setItem(index,0,QTableWidgetItem(query.value(0).toString()))
                table.setItem(index,1,QTableWidgetItem(query.value(1).toString()))
                table.setItem(index,2,QTableWidgetItem(query.value(2).toString()))
                table.setItem(index,3,QTableWidgetItem(query.value(3).toString()))
                table.setItem(index,4,QTableWidgetItem(query.value(4).toString()))
                table.setItem(index,5,QTableWidgetItem(query.value(5).toString()))
                index = index+1
    
        table.show()
        table.resize(618,360)
        table.move(240,230)
    

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

    run()




  • Lifetime Qt Champion

    Hi,

    Indeed, as @the_ wrote, it looks like the same question.

    @Gelo please don't post the same questions in multiple sub forums, one is enough.

    Closing this one.


Log in to reply