Unsolved mysql query does not take the dataset user inserted
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")
JonB last edited by JonB
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.
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
JonB last edited by
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
QSqlTableModelfor attaching your data retrieval to the database. That will have the necessary
DELETEautomatically 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
QSqlTableModelwill 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.