-2

I've been trying to create a program that registers and logs in a user, the register part should store the details into a database but I've experienced a bit of trouble with taking input from the QT text box and inserting it into my database. It doesn't take any input from the text box, so I've tried to use qtsql to help me input data, but I can't manage to get it working.

#Register

    def InsertData(self):
        db=QSqlDatabase.addDatabase("QSQLITE")
        db.setDatabaseName("/home/user/test.db")
        if db.open():
            query=QSqlQuery
            query.exec_("INSERT INTO users(email_address,password) VALUES(?,?)",self.password.text,self.email.text)
            db.commit()
            db.close()

DIRi
  • 13
  • 5
  • what is your **specific** question? Any error messages? – eyllanesc Dec 06 '20 at 00:16
  • I want my code to take the text box input and store it in my SQLite database. There is no error message but my code doesn’t store the values into the database. – DIRi Dec 06 '20 at 01:03
  • There are at least three errors in your code: you are not creating an instance of QSqlQuery and you're not getting the text of the password nor the email, in all three cases you're missing the () parentheses. – musicamante Dec 06 '20 at 01:12

1 Answers1

1

You can‘t get the value in your SQL statement, because you are not calling the object for password and email

Try:

val_password = self.password.text()
val_email = self.email.text()
query.exec_("INSERT INTO users(email_address,password) VALUES(?,?)",val_password, val_email)

I‘m not used to qtsql, but in sqlite you also have to assign a cursor to your sql connection, you might also want to check that.