First of all thx for Your help.
The database is MariaDB 5.5 on opensuse 13.1
I tried Your suggestion ChrisW67 and I converted it to bindValue but it does not help
query.prepare("INSERT INTO Ruchy (z,do,twr_id,ilosc,opis,czas,dozor_prc_id) VALUES (:z,:do,:twr_id,:ilosc,:opis,now(),:dozor_prc_id)");
query.bindValue(":z",ui->LE_ruchy_z_id->text());
query.bindValue(":do",ui->LE_ruchy_do_id->text());
query.bindValue(":twr_id",ui->LE_ruchy_twr_id->text());
query.
bindValue(":ilosc",
QString::number(ui
->SB_ruchy_ilosc
->value
()));
query.bindValue(":opis",ui->PTE_ruchy_opis->toPlainText());
query.
bindValue(":dozor_prc_id",
QString::number(::prc_id));
query.exec();
query.prepare("INSERT INTO Ruchy (z,do,twr_id,ilosc,opis,czas,dozor_prc_id) VALUES (:z,:do,:twr_id,:ilosc,:opis,now(),:dozor_prc_id)");
query.bindValue(":z",ui->LE_ruchy_z_id->text());
query.bindValue(":do",ui->LE_ruchy_do_id->text());
query.bindValue(":twr_id",ui->LE_ruchy_twr_id->text());
query.bindValue(":ilosc",QString::number(ui->SB_ruchy_ilosc->value()));
query.bindValue(":opis",ui->PTE_ruchy_opis->toPlainText());
query.bindValue(":dozor_prc_id",QString::number(::prc_id));
query.exec();
To copy to clipboard, switch view to plain text mode
the table deffinition is as follows
CREATE TABLE `Ruchy` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`z` int(11) DEFAULT NULL,
`do` int(11) DEFAULT NULL,
`twr_id` int(11) DEFAULT NULL,
`ilosc` int(11) DEFAULT NULL,
`opis` varchar(450) COLLATE utf8_unicode_ci DEFAULT NULL,
`czas` datetime DEFAULT NULL,
`dozor_prc_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `Ruchy` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`z` int(11) DEFAULT NULL,
`do` int(11) DEFAULT NULL,
`twr_id` int(11) DEFAULT NULL,
`ilosc` int(11) DEFAULT NULL,
`opis` varchar(450) COLLATE utf8_unicode_ci DEFAULT NULL,
`czas` datetime DEFAULT NULL,
`dozor_prc_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
To copy to clipboard, switch view to plain text mode
the strange think is that that the LastInsertId() works on other table sreturning the ID but not here
If I export qDebug()<< query.lastQuery() as a text to MYSQL workbench then the row is inserted without any problems - this is strange
I will attache the whole function that contains this insert bellow so You'll be able to track it maybe
void MainWindow::on_PB_ruchy_zapisz_clicked()
{
if (ui->LE_ruchy_do_id->text().isEmpty() or ui->LE_ruchy_z_id->text().isEmpty() or ui->LE_ruchy_twr_id->text().isEmpty())
{
msgBox.setText("Musisz wybrac magazyn zrodlowy, docelowy, oraz towar");
msgBox.exec();
return;
}
query.prepare("LOCK TABLE inicio.Stany WRITE");query.exec();
query.prepare("SELECT id,stan from Stany where twr_id='"+ui->LE_ruchy_twr_id->text()+"' and magazyn_id='"+ui->LE_ruchy_z_id->text()+"'" );
query.exec();
if (query.next())
{
int stan=query.value(1).toInt();
if (stan>=ui->SB_ruchy_ilosc->value())
{
query.
prepare("UPDATE Stany set stan=stan-"+QString::number(ui
->SB_ruchy_ilosc
->value
())+" where id='"+query.
value(0).
toString()+"' ");
query.exec();
}
else
{
if (ui->LE_ruchy_z_id->text()!="-2")
{
msgBox.
setText("Nie mozna wydac wiecej niz jest na stanie \n \n Na stanie: "+QString::number(stan
)+" szt.");
msgBox.exec();
return;
}
}
}
else
{
if (ui->LE_ruchy_z_id->text()=="-2")
{ //mozna wydac z zerowego stanu tylko przy zakupach - zakupy maja prc_id=-2
query.
prepare("INSERT INTO Stany (magazyn_id,twr_id,stan) VALUES ('"+ui
->LE_ruchy_z_id
->text
()+"','"+ui
->LE_ruchy_twr_id
->text
()+"','"+QString::number(0-ui
->SB_ruchy_ilosc
->value
())+"')");
query.exec();
}
else
{//jesli ktos nie ma to nie moze wydac
msgBox.setText("Nie ma tego towaru w magazynie zrodlowym wiec nie moze on go wydac");
msgBox.exec();
return;
}
}
query.prepare("INSERT INTO Ruchy (z,do,twr_id,ilosc,opis,czas,dozor_prc_id) VALUES (:z,:do,:twr_id,:ilosc,:opis,now(),:dozor_prc_id)");
query.bindValue(":z",ui->LE_ruchy_z_id->text());
query.bindValue(":do",ui->LE_ruchy_do_id->text());
query.bindValue(":twr_id",ui->LE_ruchy_twr_id->text());
query.
bindValue(":ilosc",
QString::number(ui
->SB_ruchy_ilosc
->value
()));
query.bindValue(":opis",ui->PTE_ruchy_opis->toPlainText());
query.
bindValue(":dozor_prc_id",
QString::number(::prc_id));
query.exec();
qDebug()<<"ruchy querry : "<<query.lastQuery();
qDebug()<<"Ruchy insert : "<<query.lastError();
qDebug()<<"ruchy last id: "<<query.lastInsertId().toString();
query.prepare("SELECT id from Stany where twr_id='"+ui->LE_ruchy_twr_id->text()+"' and magazyn_id='"+ui->LE_ruchy_do_id->text()+"'" );
query.exec();
if (query.next())
{
query.
prepare("UPDATE Stany set stan=stan+"+QString::number(ui
->SB_ruchy_ilosc
->value
())+" where id='"+query.
value(0).
toString()+"' ");
query.exec();
qDebug()<<"aktualizuj stan bo byl :"+query.lastQuery();
}
else
{
query.
prepare("INSERT INTO Stany (magazyn_id,twr_id,stan) VALUES ('"+ui
->LE_ruchy_do_id
->text
()+"','"+ui
->LE_ruchy_twr_id
->text
()+"','"+QString::number(ui
->SB_ruchy_ilosc
->value
())+"')");
query.exec();
qDebug()<<"dodaj stan bo nie bylo "+query.lastQuery();
qDebug()<<"stany insert : "<<query.lastError();
qDebug()<<"stany last id: "<<query.lastInsertId().toString();
}
query.prepare("UNLOCK TABLE inicio.Stany ");query.exec();
qDebug()<<"unlock table : "<<query.lastError();
query.prepare("UNLOCK TABLES");query.exec();
qDebug()<<"unlock tables : "<<query.lastError();
qDebug()<<"commit : "<<query.lastError();
void MainWindow::on_PB_ruchy_zapisz_clicked()
{
if (ui->LE_ruchy_do_id->text().isEmpty() or ui->LE_ruchy_z_id->text().isEmpty() or ui->LE_ruchy_twr_id->text().isEmpty())
{
QMessageBox msgBox;
msgBox.setText("Musisz wybrac magazyn zrodlowy, docelowy, oraz towar");
msgBox.exec();
return;
}
QSqlQuery query;
query.prepare("LOCK TABLE inicio.Stany WRITE");query.exec();
query.prepare("SELECT id,stan from Stany where twr_id='"+ui->LE_ruchy_twr_id->text()+"' and magazyn_id='"+ui->LE_ruchy_z_id->text()+"'" );
query.exec();
if (query.next())
{
int stan=query.value(1).toInt();
if (stan>=ui->SB_ruchy_ilosc->value())
{
query.prepare("UPDATE Stany set stan=stan-"+QString::number(ui->SB_ruchy_ilosc->value())+" where id='"+query.value(0).toString()+"' ");
query.exec();
}
else
{
if (ui->LE_ruchy_z_id->text()!="-2")
{
QMessageBox msgBox;
msgBox.setText("Nie mozna wydac wiecej niz jest na stanie \n \n Na stanie: "+QString::number(stan)+" szt.");
msgBox.exec();
return;
}
}
}
else
{
if (ui->LE_ruchy_z_id->text()=="-2")
{ //mozna wydac z zerowego stanu tylko przy zakupach - zakupy maja prc_id=-2
query.prepare("INSERT INTO Stany (magazyn_id,twr_id,stan) VALUES ('"+ui->LE_ruchy_z_id->text()+"','"+ui->LE_ruchy_twr_id->text()+"','"+QString::number(0-ui->SB_ruchy_ilosc->value())+"')");
query.exec();
}
else
{//jesli ktos nie ma to nie moze wydac
QMessageBox msgBox;
msgBox.setText("Nie ma tego towaru w magazynie zrodlowym wiec nie moze on go wydac");
msgBox.exec();
return;
}
}
query.prepare("INSERT INTO Ruchy (z,do,twr_id,ilosc,opis,czas,dozor_prc_id) VALUES (:z,:do,:twr_id,:ilosc,:opis,now(),:dozor_prc_id)");
query.bindValue(":z",ui->LE_ruchy_z_id->text());
query.bindValue(":do",ui->LE_ruchy_do_id->text());
query.bindValue(":twr_id",ui->LE_ruchy_twr_id->text());
query.bindValue(":ilosc",QString::number(ui->SB_ruchy_ilosc->value()));
query.bindValue(":opis",ui->PTE_ruchy_opis->toPlainText());
query.bindValue(":dozor_prc_id",QString::number(::prc_id));
query.exec();
qDebug()<<"ruchy querry : "<<query.lastQuery();
qDebug()<<"Ruchy insert : "<<query.lastError();
qDebug()<<"ruchy last id: "<<query.lastInsertId().toString();
query.prepare("SELECT id from Stany where twr_id='"+ui->LE_ruchy_twr_id->text()+"' and magazyn_id='"+ui->LE_ruchy_do_id->text()+"'" );
query.exec();
if (query.next())
{
query.prepare("UPDATE Stany set stan=stan+"+QString::number(ui->SB_ruchy_ilosc->value())+" where id='"+query.value(0).toString()+"' ");
query.exec();
qDebug()<<"aktualizuj stan bo byl :"+query.lastQuery();
}
else
{
query.prepare("INSERT INTO Stany (magazyn_id,twr_id,stan) VALUES ('"+ui->LE_ruchy_do_id->text()+"','"+ui->LE_ruchy_twr_id->text()+"','"+QString::number(ui->SB_ruchy_ilosc->value())+"')");
query.exec();
qDebug()<<"dodaj stan bo nie bylo "+query.lastQuery();
qDebug()<<"stany insert : "<<query.lastError();
qDebug()<<"stany last id: "<<query.lastInsertId().toString();
}
query.prepare("UNLOCK TABLE inicio.Stany ");query.exec();
qDebug()<<"unlock table : "<<query.lastError();
query.prepare("UNLOCK TABLES");query.exec();
qDebug()<<"unlock tables : "<<query.lastError();
qDebug()<<"commit : "<<query.lastError();
To copy to clipboard, switch view to plain text mode
Bookmarks