解决前:
QByteArray SymbolManager::getSvgData(const QString &name) {
QSqlQuery query(db);
//query.prepare(QString("SELECT images_name FROM symbols WHERE name = '%1'").arg(name));
query.prepare("SELECT images_name FROM images WHERE name = :name");
query.bindValue(":name", name);
if (!query.exec()) {
qDebug() << "查询失败:" << query.lastError().text();
qDebug() << "Query:" << query.lastQuery();
qDebug() << "Values:" << query.boundValues();
return QByteArray();
}
if (query.next()) {
QByteArray data = query.value(0).toByteArray();
qDebug() << "读取到的SVG数据:" << data;
return data;
} else {
qDebug() << "没有找到名为" << name << "的SVG数据";
return QByteArray();
}
}
输出结果:
查询失败: " Parameter count mismatch"
Query: "SELECT images_name FROM images WHERE name = :name"
Values: QMap((":name", QVariant(QString, "五角星")))
SVG data Errow
问题:
query.exec()中没有给查询语句: "SELECT images_name FROM images" 解决后:
QByteArray SymbolManager::getSvgData(const QString &name) {
QSqlQuery query(db);
//query.prepare(QString("SELECT images_name FROM symbols WHERE name = '%1'").arg(name));
query.prepare("SELECT images_name FROM images WHERE name = :name");
query.bindValue(":name", name);
if (!query.exec("SELECT images_name FROM images")) {
qDebug() << "查询失败:" << query.lastError().text();
qDebug() << "Query:" << query.lastQuery();
qDebug() << "Values:" << query.boundValues();
return QByteArray();
}
if (query.next()) {
QByteArray data = query.value(0).toByteArray();
qDebug() << "读取到的SVG数据:" << data;
return data;
} else {
qDebug() << "没有找到名为" << name << "的SVG数据";
return QByteArray();
}
}
输出:
读取到的SVG数据: "\xE5\x9C\x86\xE5\xBD\xA2"
SVG data: "\xE5\x9C\x86\xE5\xBD\xA2"