I am new to Qt SQL, we need to insert data row by row into database.
First I need to fetch data from the application into a row, a row consists of 4 types of data: int, float, string & null. Then I insert this row into db.
To compare different methods speeds, we tried 2 solutions:
- Put all data of a row into
std::vector<QVariant>
, then usebindValue
method to insert to db:
std::vector<QVariant> getOneRow(int row)
{
std::vector<QVariant> outPut;
// Get int 1, float 1.23, string "I am string", "NULL" from the application
outPut.push_back(1);
outPut.push_back(1.23);
outPut.push_back("I am string");
outPut.push_back("NULL");
return outPut;
}
void insertDataBase(std::vector<QVariant> input, QString query)
{
int rowLength = input.size();
myQtSql.prepare(query);
for(int i = 0; i < rowLength ; ++i)
{
myQtSql.bindValue(i,input[i]);
}
myQtSql.exec();
}
- Convert every element of a row to QString, then use "INSERT INTO" statement into db:
QString getOneRow(int row)
{
QString outPut;
// Get int 1, float 1.23, string "I am string", "NULL" from the application
outPut += QString::number(1);
outPut += ",";
outPut += QString::number(1.23, 'f', 2);
outPut += ",";
outPut += "I am string";
outPut += ",";
outPut += "NULL";
return outPut;
}
void insertDataBase(QString input)
{
QString query="INSERT INTO MyTable VALUES("+input+");";
myQtSql.exec(query))
}
The whole workflow is like:
while(row < maxRow) //maxRow, like 1 million
{
getOneRow(row);
insertDataBase();
}
The advantage of method 1 is getOneRow
execution time is 1/10 of that in method 2, since I don't need to convert every numeric value into QString (I found that QString::number is very time consuming, which takes almost 90% of the execution time.) But bindValue
method is slower than "INSERT INTO"
The advantage of method 2 is "INSERT INTO" is much faster than bindValue
method, overall method 2 execution time is half of method 1. I tested under Desktop Qt 6.1.3 MSVC 2019 64-bit, release mode
May I ask: 1. Are there other better ways? 2. Since QString::number is very time consuming, are there better value string conversion methods? I found Dragonbox Fastest C++ way to convert float to string
Thank you very much