#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDate>
#include <QTime>
#include <QDebug>
int main(int argc, char **argv) {
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("test");
db.setPassword("");
if (db.open()) {
// Set up a test table
if (qry.exec("CREATE TEMPORARY TABLE atable(id INTEGER, d DATE, t TIME, v INTEGER)")) {
db.transaction();
stopwatch.start();
qry.prepare("INSERT INTO atable VALUES(?, ?, ?, ?)");
for (int i = 0; i < 10000; ++i) {
qry.bindValue(0, i);
qry.bindValue(1, testDate);
qry.bindValue(2, testTime);
qry.bindValue(3, i);
if (!qry.exec())
qDebug() << qry.lastError();
}
qDebug() << stopwatch.elapsed();
db.commit();
}
else
qDebug() << qry.lastError();
}
else
qDebug() << db.lastError();
return 0;
}
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
#include <QDate>
#include <QTime>
#include <QDebug>
int main(int argc, char **argv) {
QCoreApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("test");
db.setPassword("");
if (db.open()) {
// Set up a test table
QSqlQuery qry;
if (qry.exec("CREATE TEMPORARY TABLE atable(id INTEGER, d DATE, t TIME, v INTEGER)")) {
QTime stopwatch;
QDate testDate = QDate::currentDate();
QTime testTime = QTime::currentTime();
db.transaction();
stopwatch.start();
qry.prepare("INSERT INTO atable VALUES(?, ?, ?, ?)");
for (int i = 0; i < 10000; ++i) {
qry.bindValue(0, i);
qry.bindValue(1, testDate);
qry.bindValue(2, testTime);
qry.bindValue(3, i);
if (!qry.exec())
qDebug() << qry.lastError();
}
qDebug() << stopwatch.elapsed();
db.commit();
}
else
qDebug() << qry.lastError();
}
else
qDebug() << db.lastError();
return 0;
}
To copy to clipboard, switch view to plain text mode
Does 10000 inserts in 2.8 seconds over a 100Mbps LAN to a remote server.
Bookmarks