QSqlQuery 插入数据库不起作用

QSqlQuery Insert into database doesn't work

我用PyQt4.QtSql.QSqlQuery。我从表单中获取值并想将它们插入到数据库中。我打开一个连接并尝试添加,但数据库中没有值,也没有错误。有什么问题?

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

您必须commit您所做的更改:

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