C++ (Qt)const QSqlQuery SQLStoredExecute(const QSqlDatabase &database, const QString &storedFunct, const QMap<QString, QVariant> &variables = QMap<QString, QVariant>()){ QSqlQuery stored(database); if (variables.isEmpty()) stored.prepare(storedFunct); else stored.prepare(QString("{CALL %1(?%2)}") .arg(storedFunct) .arg(QString(", ?").repeated(variables.size()-1))); for (QMap<QString, QVariant>::const_iterator i = variables.constBegin(); i!=variables.constEnd(); ++i) stored.bindValue(i.key(), i.value()); stored.exec(); return stored;} int SQLInsert(const QSqlDatabase &database, const QString &table, const QMap<QString, QVariant>&variables){ if (variables.isEmpty()) return -1; QStringList columns = variables.keys(); QString queryStr = QString("INSERT INTO %1 (%2) VALUES (:%3) SELECT @@IDENTITY") .arg(table) .arg(columns.join(", ")) .arg(columns.join(", :")); QSqlQuery query(m_Connect); query.prepare(queryStr); for (QMap<QString, QVariant>::const_iterator i = variables.constBegin(); i!=variables.constEnd(); ++i) query.bindValue(i.key(), i.value()); if (query.exec()) { if (query.next()) return query.value(0).toInt(); return 0; } return -1;} bool SQLUpdate(const QSqlDatabase &database, const QString &table, const int &id, const QMap<QString, QVariant> &variables){ if (variables.isEmpty()) return false; QStringList columns = variables.keys(); for (int i=0; i<columns.size(); ++i) columns[i] = QString("%1=:%1").arg(columns.at(i)); QString queryStr = QString("UPDATE %1 SET %2 WHERE id = %3") .arg(table) .arg(columns.join(", ")) .arg(id); QSqlQuery query(database); query.prepare(queryStr); for (QMap<QString, QVariant>::const_iterator i = variables.constBegin(); i!=variables.constEnd(); ++i) query.bindValue(i.key(), i.value()); return query.exec();} bool SQLDelete(const QSqlDatabase &database, const QString &table, const int &id){ QSqlQuery query(database); QString set = QString("DELETE FROM %1 WHERE id = %2").arg(table).arg(id); return query.exec(set);} const QSqlQuery SQLStoredExecute(const QString &connectionName, const QString &storedFunct, const QMap<QString, QVariant> &variables = QMap<QString, QVariant>()){ return SQLStoredExecute(QSqlDatabase::database(connectionName), storedFunct, variables);} int SQLInsert(const QString &connectionName, const QString &table, const QMap<QString, QVariant>&variables){ return SQLInsert(QSqlDatabase::database(connectionName), table, variables);} bool SQLUpdate(const QString &connectionName, const QString &table, const int &id, const QMap<QString, QVariant> &variables){ return SQLUpdate(QSqlDatabase::database(connectionName), table, id, variables);} bool SQLDelete(const QString &connectionName, const QString &table, const int &id){ return SQLDelete(QSqlDatabase::database(connectionName), table, id);}
declare @t table(id_col int); INSERT INTO %1 (%2) OUTPUT INSERTED.id into @t VALUES (:%3); select * from @t