QSqlQuery.exec(查询)returns unicode 字符的错误字符
QSqlQuery.exec(query) returns wrong character for unicode chars
我在 MS SQL 服务器上有一个数据库和我的问题
是unicode字符!
"query.exec(str)" 在我的 "getRecordByCondition" 方法中 returns “?” nvarchar 字段的 char!
但它在 "loadToGrid" 方法中正常工作。
这是我的代码:
void DataAccess::connect()
{
db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("TestDB");
}
void DataAccess::disConnect()
{
QString connection;
connection = db.connectionName();
db.close();
}
QVariantList DataAccess::getRecordByCondition(QString tableName, QString condition)
{
connect();
QVariantList recordData;
QString str= " select * from " + tableName + " where " + condition ;
QSqlRecord record;
QSqlQuery query(db);
if(db.open())
{
// can run
if(query.exec(str))
{
record=query.record();
int cols=record.count();
while (query.next())
{
for(int i=0;i<cols;i++)
{
qDebug() << query.value(i); // ??????? ????
recordData << query.value(i);
}
}
disConnect();
//return 1;
}
else
{
qDebug() << "problem!" << db.lastError();
disConnect();
//return -1;
}
}
else
{
qDebug() << "db can not open because:" << db.lastError().text();
//return -2;
}
disConnect();
return recordData;
}
void DataAccess::loadToGrid(QString tableName, QTableView &grid)
{
connect();
if(db.open())
{
//Load data
QString qry="select * from " + tableName;
QSqlRecord record;
QSqlQuery query(db);
if(query.exec(qry))
{
record=query.record();
int cols=record.count();
int rows=0;
//make model
QStandardItemModel *myModel=new QStandardItemModel(0,cols,grid.parent());
//make headers
for(int i=0;i<cols;i++)
{
myModel->setHorizontalHeaderItem(i,new QStandardItem(record.fieldName(i)));
}
while (query.next()) {
for(int i=0;i<cols;i++)
{
myModel->setItem(rows,i,new QStandardItem(query.value(i).toString()));
}
rows++;
}
grid.setModel(myModel);
}
}
disConnect();
}
我解决了这个问题,
我只改变了我的连接方式
void DataAccess::connect()
{
if(!db.open())
{
db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("TCOM");
}
}
我在 MS SQL 服务器上有一个数据库和我的问题 是unicode字符! "query.exec(str)" 在我的 "getRecordByCondition" 方法中 returns “?” nvarchar 字段的 char! 但它在 "loadToGrid" 方法中正常工作。 这是我的代码:
void DataAccess::connect()
{
db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("TestDB");
}
void DataAccess::disConnect()
{
QString connection;
connection = db.connectionName();
db.close();
}
QVariantList DataAccess::getRecordByCondition(QString tableName, QString condition)
{
connect();
QVariantList recordData;
QString str= " select * from " + tableName + " where " + condition ;
QSqlRecord record;
QSqlQuery query(db);
if(db.open())
{
// can run
if(query.exec(str))
{
record=query.record();
int cols=record.count();
while (query.next())
{
for(int i=0;i<cols;i++)
{
qDebug() << query.value(i); // ??????? ????
recordData << query.value(i);
}
}
disConnect();
//return 1;
}
else
{
qDebug() << "problem!" << db.lastError();
disConnect();
//return -1;
}
}
else
{
qDebug() << "db can not open because:" << db.lastError().text();
//return -2;
}
disConnect();
return recordData;
}
void DataAccess::loadToGrid(QString tableName, QTableView &grid)
{
connect();
if(db.open())
{
//Load data
QString qry="select * from " + tableName;
QSqlRecord record;
QSqlQuery query(db);
if(query.exec(qry))
{
record=query.record();
int cols=record.count();
int rows=0;
//make model
QStandardItemModel *myModel=new QStandardItemModel(0,cols,grid.parent());
//make headers
for(int i=0;i<cols;i++)
{
myModel->setHorizontalHeaderItem(i,new QStandardItem(record.fieldName(i)));
}
while (query.next()) {
for(int i=0;i<cols;i++)
{
myModel->setItem(rows,i,new QStandardItem(query.value(i).toString()));
}
rows++;
}
grid.setModel(myModel);
}
}
disConnect();
}
我解决了这个问题, 我只改变了我的连接方式
void DataAccess::connect()
{
if(!db.open())
{
db=QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("TCOM");
}
}