SOLVED : SqlQuery can't insert data.
Hello, I am trying to insert a new record into my db from lineedits. Everything works fine. But if there is some word in Double quotes (" ") the query fails with Sql syntax error. How can i insert data that contains double quotes?
Code:
void MainWindow::insertRow()
{
QString sqlstr
=QString("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(\"%1\",\"%2\",\"%3\",\"%4\",\"%5\");") .arg(le2->text())
.arg(le1->text())
.arg(le3->text())
.arg(le4->text())
.arg(le5->text());
if(q->exec(sqlstr)==false)
{
pmsg->setText("can't connect with db");
pmsg->setInformativeText(q->lastError().text());
pmsg->exec();
return;
}
delete q;
}
Re: SqlQuery can't insert data.
Use named placeholders, something like this :
Code:
q->prepare("INSERT INTO sianimtable (CIPHER,CIPHERKP,TITLE,MSGON,MSGOFF) VALUES(:le1,:le2,:le3,:le4,:le5);");
q->bindValue(":le1",le1->text());
q->bindValue(":le2",le2->text());
q->bindValue(":le3",le3->text());
q->bindValue(":le4",le4->text());
q->bindValue(":le5",le5->text());
if(q->exec(sqlstr)==false)
{
pmsg->setText("can't connect with db");
pmsg->setInformativeText(q->lastError().text());
pmsg->exec();
}
delete q;