bool MdiChild::save(){ command = "delete from " + dbName + ";"; QSqlQuery queryDelete (command); char sql[1024]; QList<DnDLabel *> labels = mapWidget->findChildren<DnDLabel *>(); for (int i = 0; i < labels.size(); ++i) { char *data = labels.at(i)->ba.data(); command = "INSERT INTO " + dbName + " VALUES (NULL, %d, %5.5f, %5.5f,%d,%d,%d,%d,'%s');"; sprintf(sql,command, data[0], // device id labels.at(i)->val_x, // point X labels.at(i)->val_y, // point Y 0, // angle data[1], // camera number data[2], // address number data[3], // cable number ""); QSqlQuery querySave (sql); } return true;}
bool MdiChild::save(){ QSqlQuery queryDelete; command = "DELETE FROM " + dbName; queryDelete.exec(command); QList<DnDLabel *> labels = mapWidget->findChildren<DnDLabel *>(); for (int i = 0; i < labels.size(); ++i) { QSqlQuery querySave; command = "INSERT INTO " + dbName + " (NUMBER, DEVICE_ID, VAL_X, VAL_Y, ANGLE, CAMERA_NUMBER, ADDRESS, \CABLE, DESCRIPTION) VALUES (:NUMBER, :DEVICE_ID, :VAL_X, :VAL_Y, :ANGLE, :CAMERA_NUMBER, :ADDRESS, :CABLE, :DESCRIPTION)"; querySave.prepare(command); char* data = labels.at(i)->ba.data(); querySave.bindValue(":NUMBER", 0); querySave.bindValue(":DEVICE_ID", data[0]); querySave.bindValue(":VAL_X", labels.at(i)->val_x); querySave.bindValue(":VAL_Y", labels.at(i)->val_y); querySave.bindValue(":ANGLE", 0); querySave.bindValue(":CAMERA_NUMBER", data[1]); querySave.bindValue(":ADDRESS", data[2]); querySave.bindValue(":CABLE", data[3]); querySave.bindValue(":DESCRIPTION", ""); querySave.exec(); } return true;}
To bind a NULL value, use a null QVariant; for example, use QVariant(QVariant::String) if you are binding a string.
bool MdiChild::save(){ command = "delete from " + dbName + ";"; QSqlQuery queryDelete (command); QList<DnDLabel *> labels = mapWidget->findChildren<DnDLabel *>(); for (int i = 0; i < labels.size(); ++i) { if ( labels.at(i)->objectName() != "disabled") { char *data = labels.at(i)->ba.data(); QString id = QString::number(data[0]); QString point_X = QString::number(labels.at(i)->val_x); QString point_Y = QString::number(labels.at(i)->val_y); QString angle = QString::number(0); QString camera = QString::number(data[1]); QString address = QString::number(data[2]); QString cable = QString::number(data[3]); QString description = ""; command = QString("INSERT INTO " + dbName + " VALUES (NULL, %1, %2, %3,%4,%5,%6,%7,'%8')").arg(id).arg(point_X).arg(point_Y).arg(angle).arg(camera).arg(address).arg(cable).arg(description); QSqlQuery querySave (command); } } return true;}