Afams
Afams

Reputation: 47

Qt & SqlLite inset data to database

I'm doing Qt application. I am trying save data in database. I have got problem because I have got connection with sqlite3, but when i want insert data QT says that 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"));
    }
}

It is results.

QSqlQuery::prepare: database not open
Succsess

Can someone help me?

Upvotes: 0

Views: 468

Answers (1)

eyllanesc
eyllanesc

Reputation: 244043

Your main error is that you are trying to create the table before opening the database, that's why you get that error.

Another error that is repetitive is that even when a problem happens your logic will still want to insert data, what you should do is print an error message and return.

You also have errors in the query, for example in the creation of the table you need to close with a parenthesis.

And finally do not build the query directly if you are going to use data provided by the user since your system will be prone to SQL Injection, it is appropriate to use the 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"));

Upvotes: 2

Related Questions