QSqlQuery (sqlite engine), how to bind chinese code field?
-
os: windows 10
compiler: vs 2017
code: utf8 with bomsql:
SELECT material_t.*,( SELECT material_attr_t.mtavalue FROM material_attr_t ,material_type_attr_t WHERE material_attr_t.mtaid = material_type_attr_t.mtaid AND mtaname = '阻值' AND material_attr_t.mid = material_t.mid ) AS '阻值', WHERE material_t.mtid = 1 AND 阻值=:阻值
prepare:
sqlQuery.prepare( QString::fromLocal8Bit( prepare.c_str() )bind:
sqlQuery.bindValue( ":" + QString::fromLocal8Bit(it->col.c_str()), it->d);
it is actual bind :
sqlQuery.bindValue( ":" + QString::fromLocal8Bit("阻值"), 100);QMap<QString, QVariant> bound = sqlQuery.boundValues();
i find bound's size is 0. so ,bind failture?
then ,how can i to bind chinese charactor field?
-
@changtj said in QSqlQuery (sqlite engine), how to bind chinese code field?:
QString::fromLocal8Bit
Why do you call this with a string containing Chinese characters?!
Chenese can't be represented with 8bit encodings.
Use fromUtf8() instead. -
it is not ok, i changed code as follow.
prepare:
sqlQuery.prepare( QString::fromUtf8( prepare.c_str() ) )qDebug output:
SELECT material_t.*,( SELECT material_attr_t.mtavalue FROM material_attr_t ,material_type_attr_t WHERE material_attr_t.mtaid = material_type_attr_t.mtaid AND mtaname = '阻值' AND mtashow = 1 AND material_attr_t.mid = material_t.mid ) AS '阻值' WHERE material_t.mtid = 1 AND 阻值=:阻值bind:
QString placeholder = ":" + QString::fromUtf8(it->col.c_str());
qDebug() << placeholder << "," << it->d << "; ";
sqlQuery.bindValue( placeholder, it->d);qDebug output:
":阻值" , QVariant(int, 100) ;sqlQuery exec error: " Parameter count mismatch"