Python Question

QSqlQuery Insert into database doesn't work

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

Answer

You must commit the changes you have made:

if query.exec_():
    db.commit() 
else:
    QMessageBox.warning(None, "Database Error",
        query.lastError().text())