Graphics Engineer
Graphics Engineer

Reputation: 105

Execution of adding SQL code is not happening in Qt

When I try to add data to data base through INSERT INTO, it isn't adding the data. The database is connected but the execution is not happening

Same database is connected on another window, I tried to disconnected it and then connect it on new page this still shows the same error

void signup::on_pushButton_clicked()
{
    sql = QSqlDatabase::addDatabase("QSQLITE","SQLITE");
    sql.setDatabaseName("/E:/OOP Project/casinousers.db");
    if(sql.open()) {
       qDebug() << "Database connected";
    }
    else {
      qDebug() << "Error in connecting";
    }
    QString email,pass,name;
    int amount;
    email  = ui->lineEdit->text();
    pass = ui->lineEdit_2->text();
    name = ui->lineEdit_3->text();
    amount = ui->lineEdit_4->text().toInt();
    QSqlQuery *query = new QSqlQuery(sql);
    query->prepare("INSERT INTO Data(Email,Password,name,Amount)" "VALUES(':email',':pass',':name',:amount)");
    query->bindValue(":email",email);
    query->bindValue(":pass",pass);
    query->bindValue(":name",name);
    query->bindValue(":amount",amount);
    if(!query->exec()) {
        qDebug() <<" error";
    }
}

The output is "error" - it's not adding the data

Upvotes: 0

Views: 83

Answers (1)

Graphics Engineer
Graphics Engineer

Reputation: 105

The database was locked.

I unlocked it and this solved the problem.

I came to know about this problem by printing the actual error

qDebug() << query->lastError();

Upvotes: 2

Related Questions