Qt& SqlLite将数据插入数据库

时间:2018-06-14 15:40:48

标签: c++ qt sqlite qt5 qtsql

我正在做Qt应用程序。我正在尝试在数据库中保存数据。我有问题,因为我已经连接了sqlite3,但是当我想要插入数据时QT说QSqlQuery :: prepare:数据库没有打开。

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

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

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

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

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

如果您打算使用用户提供的数据,最后不要直接构建查询,因为您的系统很容易使用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"));