Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. mysql query does not take the dataset user inserted
QtWS25 Last Chance

mysql query does not take the dataset user inserted

Scheduled Pinned Locked Moved Unsolved General and Desktop
mysqlcursorpython3python
4 Posts 2 Posters 617 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lanas
    wrote on last edited by
    #1

    I was trying to insert or retrieve data from database but the function failed every time i'm taking data from user and put it in the database the fucntion failed in the cursosr.excute i do not know if the problem from the query or how i take the values of inserting data ,i'm developing my application using python 3.9 qt5 and mysql

    this is my function

    def SignupFunction(self):
          try:
    
            connection = mc.connect(host=cr.host, user=cr.user, password=cr.password, database=cr.database)
    
            cur = connection.cursor()
            
    
            cur.execute(
                "insert into users (first_name , last_name, email,password , confirm_paswword ,answer) value( %s, %s, %s, %s, %s, %s)",
                (
                    self.first_name.text(),
                    self.last_name.text(),
                    self.email.text(),
                    self.password.text(),
                    self.confirm_paswword.text(),
                    self.answer.text()
                ))
            
    
            connection.commit()
    
            connection.close()
            self.labelResult.setText("Data Inserted ")
          except Exception as e:
            self.labelResult.setText("Error Inserting Data")
    
    JonBJ 1 Reply Last reply
    0
    • L lanas

      I was trying to insert or retrieve data from database but the function failed every time i'm taking data from user and put it in the database the fucntion failed in the cursosr.excute i do not know if the problem from the query or how i take the values of inserting data ,i'm developing my application using python 3.9 qt5 and mysql

      this is my function

      def SignupFunction(self):
            try:
      
              connection = mc.connect(host=cr.host, user=cr.user, password=cr.password, database=cr.database)
      
              cur = connection.cursor()
              
      
              cur.execute(
                  "insert into users (first_name , last_name, email,password , confirm_paswword ,answer) value( %s, %s, %s, %s, %s, %s)",
                  (
                      self.first_name.text(),
                      self.last_name.text(),
                      self.email.text(),
                      self.password.text(),
                      self.confirm_paswword.text(),
                      self.answer.text()
                  ))
              
      
              connection.commit()
      
              connection.close()
              self.labelResult.setText("Data Inserted ")
            except Exception as e:
              self.labelResult.setText("Error Inserting Data")
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @lanas
      Hello and welcome.

      This is a Qt forum. Since you are using Python only code/library to access your SQL database, nothing Qt here at all (Qt has SQL database classes, but you are not using them), this would be best on a Python forum.

      There could be any number of problems in what you have, and since you don't do any diagnostics/fetching errors nobody can tell.

      Though for one thing there is no such SQL statement as insert ... value (), so if you want things which will work you need to copy examples which have correct SQL syntax, else they won't work.

      BTW, going back to the Python vs Qt SQL access. You may be more comfortable with the Python SQL library you are using above than the Qt classes for SQL access and not be thinking of changing. But especially if you intend to show the user the results using Qt widgets --- like in tables etc. --- you will have a to do quite a bit of work to get your Python result sets in & out of the UI. Moving to Qt's SQL classes will integrate much more easily into Qt's UI.

      1 Reply Last reply
      1
      • L Offline
        L Offline
        lanas
        wrote on last edited by
        #3

        @JonB said in mysql query does not take the dataset user inserted:

        Qt's SQL
        i'm watching a pyqt GUI development cours so i think that the best way to do it i did not know that there are qtsql and it's will be hard to retrive data from data base if i did not work with qtsql classes
        last question please is that all the documentation about the qtsql :https://doc.qt.io/qtforpython/overviews/sql-programming.html

        JonBJ 1 Reply Last reply
        0
        • L lanas

          @JonB said in mysql query does not take the dataset user inserted:

          Qt's SQL
          i'm watching a pyqt GUI development cours so i think that the best way to do it i did not know that there are qtsql and it's will be hard to retrive data from data base if i did not work with qtsql classes
          last question please is that all the documentation about the qtsql :https://doc.qt.io/qtforpython/overviews/sql-programming.html

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @lanas
          Yes to all of that, by all means read that overview link. I didn't say that "will be hard to retrive data from data base if i did not work with qtsql classes". Using the Python SQL classes instead would be fine for retrieving data from the database. But when you want to display that data in your Qt UI, rows & columns from your SQL queries, that will require some coding to get the Python-SQL-fetched-data to the Qt UI widgets and back. Whereas if you use the Qt classes they fill Qt models directly, and those are what Qt QTableViews use to display (and even edit) data.

          In the simplest case, see if you can't use Qt's QSqlTableModel for attaching your data retrieval to the database. That will have the necessary SELECT/INSERT/UPDATE/DELETE automatically generated/issued for you for working on a SQL table in the database, without you having to write the SQL statements like you tried to in your question. And attaching a QTableView to the QSqlTableModel will display what you fetched without any code. And it can even allow you/the user to edit the data in the table and write ot back to the database for you.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved