【问题标题】:Qt & SqlLite inset data to databaseQt & Sqlite 将数据插入数据库
【发布时间】:2018-11-24 10:56:47
【问题描述】:

我正在做 Qt 应用程序。我正在尝试将数据保存在数据库中。我遇到了问题,因为我已经与 sqlite3 建立了连接,但是当我想插入数据时,QT 说 QSqlQuery::prepare: database not open.

void DodajKontakt::on_btn_add_clicked()
{

    QSqlDatabase db_kon = QSqlDatabase::addDatabase("QSQLITE");
    db_kon.setDatabaseName("C:/Users/Lukasz/Desktop/qt master/organizator/baza_kontakty.db");

    QSqlQuery query;
    query.prepare( "CREATE TABLE kontakty(ids INTEGER PRIMARY KEY, name TEXT, surname TEXT, company TEXT, email TEXT, phone TEXT");

    if(!db_kon.open())
    {
        qDebug() << "error:" << db_kon.lastError().text();
    }
    else
        qDebug() << "Succsess";

    if(ui->lineEdit_name->text().isEmpty() && ui->lineEdit_surname->text().isEmpty()
            && ui->lineEdit_email->text().isEmpty() && ui->lineEdit_phone->text().isEmpty()
            && ui->lineEdit_company->text().isEmpty())
    {
        QMessageBox::critical(this, tr("Error"), tr("Uzupełnij wszystkie pola!"));

    }
    else
    {
        QString name, surname, company, email, phone;
        name = ui ->lineEdit_name->text();
        surname = ui ->lineEdit_surname->text();
        company = ui ->lineEdit_company->text();
        email = ui ->lineEdit_email->text();
        phone = ui ->lineEdit_phone->text();



        query.prepare("INSERT INTO kontakty(name,surname,company.email.phone) VALUES('"+name+"','"+surname+"','"+company+"','"+email+"','"+phone+"')");
        if(query.exec())
            QMessageBox::information(this, tr("Sukces"),tr("Dodano nowy kontakt"));
        else
            QMessageBox::information(this, tr("Error"),tr("Nie udalo sie dodac nowego kontaktu"));
    }
}

这是结果。

QSqlQuery::prepare: database not open
Succsess

有人可以帮我吗?

【问题讨论】:

    标签: c++ qt sqlite qt5 qtsql


    【解决方案1】:

    您的主要错误是您试图在打开数据库之前创建表,这就是您收到该错误的原因。

    另一个重复的错误是,即使发生问题,您的逻辑仍然需要插入数据,您应该做的是打印错误消息并返回。

    您在查询中也有错误,例如在创建表时需要用括号括起来。

    最后,如果你要使用用户提供的数据,不要直接构建查询,因为你的系统容易发生 SQL 注入,使用 bind-value 是合适的。

    QSqlDatabase db_kon = QSqlDatabase::addDatabase("QSQLITE");
    db_kon.setDatabaseName("C:/Users/Lukasz/Desktop/qt master/organizator/baza_kontakty.db");
    
    if(!db_kon.open()){
        qDebug() << "error:" << db_kon.lastError().text();
        return;
    }
    
    qDebug() << "Success";
    
    QSqlQuery query;
    query.prepare( "CREATE TABLE kontakty(ids INTEGER PRIMARY KEY, name TEXT, surname TEXT, company TEXT, email TEXT, phone TEXT)");
    
    if(!query.exec()){
        qDebug()<<"error:" << query.lastError().text();
        return;
    }
    
    QString name = ui->lineEdit_name->text();
    QString surname = ui->lineEdit_surname->text();
    QString company = ui->lineEdit_company->text();
    QString email = ui->lineEdit_email->text();
    QString phone = ui->lineEdit_phone->text();
    
    if(name.isEmpty() &&
            surname.isEmpty() &&
            email.isEmpty() &&
            phone.isEmpty() &&
            company.isEmpty())
    {
        QMessageBox::critical(this, tr("Error"), tr("Complete all fields!"));
        return;
    }
    
    query.prepare("INSERT INTO kontakty(name, surname, company, email, phone) VALUES(?, ?, ?, ?, ?)");
    
    query.addBindValue(name);
    query.addBindValue(surname);
    query.addBindValue(company);
    query.addBindValue(email);
    query.addBindValue(phone);
    
    if(query.exec())
        QMessageBox::information(this, tr("Success"),tr(" A new contact has been added"));
    else
        QMessageBox::information(this, tr("Error"),tr("It was not possible to add a new contact"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      相关资源
      最近更新 更多