2

I use PyQt4.QtSql.QSqlQuery. I get values from a Form and want to Insert them into the database. I open a connection and try to Add, but there are no values in the database and no errors. What's the problem?

    db      = QSqlDatabase.addDatabase("QMYSQL")

    db.setHostName("localhost")
    db.setDatabaseName("vista")
    db.setUserName("root")
    db.setPassword("secret")

    if (db.open()==False):     
        QMessageBox.critical(None, "Database Error",
            db.lastError().text())   
    query = QSqlQuery()
    query.prepare("INSERT INTO user (fio, sex,polis,document,birtday) "
      "VALUES (:fio, :sex,:polis,:document,:birtday)");
    query.bindValue(":fio", fio);
    query.bindValue(":sex", sex);
    query.bindValue(":polis", polis);
    query.bindValue(":document", document);
    query.bindValue(":birtday", birtday);
    query.exec_();

1 Answer 1

3

You must commit the changes you have made:

if query.exec_():
    db.commit() 
else:
    QMessageBox.warning(None, "Database Error",
        query.lastError().text())   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.