How to deal with Dynamic data entry? how to work with this!



  • import mysql.connector

    con = mysql.connector.connect(user="root",password="admingelo",host="localhost",database="dbinsert")

    m = con.cursor()

    keyword = 'heypeybeyday'

    m.execute("INSERT INTO spo (name) VALUES (%s)",(keyword))

    con.commit()

    i alway get an error when i add keyword


  • Moderators

    @Gelo

    i alway get an error when i add keyword

    may you tell us the error?

    and also you should provide a little bit more info, like what Qt version, what platform, or even what programming language you are using (even when most people may deduce it from pieces)



  • @raven-worx
    Traceback (most recent call last):
    File "C:\Users\angelo\Desktop\MySQLdb.py", line 11, in <module>
    m.execute(insert_this)
    File "C:\Python27\lib\site-packages\mysql\connector\cursor.py", line 529, in execute
    stmt = operation.encode(self._connection.python_charset)
    AttributeError: 'tuple' object has no attribute 'encode'

    pyqt4 python 2.713


  • Moderators

    @Gelo
    try:

    m.execute("INSERT INTO spo (name) VALUES (%s)", (keyword, ) )
    


  • @raven-worx i get the same error :\


  • Moderators

    @Gelo
    why are you using a tuple anyway when you only have one parameter in your query?



  • @raven-worx i try it with multiple entry but i got the same error again :3


  • Moderators

    @Gelo
    The following shouldn't give you the same error...

    m.execute("INSERT INTO spo (name) VALUES (%s)", keyword )
    


  • @raven-worx can you help how to get data in QLineEdit going to my database by Clicking Submit button? here is my code i am using python 2.17.13

    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()


Log in to reply
 

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