如何在 SQL Server Express 表中保存 QTime 并读取它? [英] How to save QTime in SQL Server Express table and read it back?
问题描述
我在 SQL Server Express 中有一个表,其中包含字段 name varchar(10)
和 timeVar time
,我想保存 QTime
的值time
字段中的 code> 变量.
I have a table in SQL Server Express with fields name varchar(10)
and timeVar time
and I want to save the value of a QTime
variable in the time
field.
这是我尝试过的:
QTime time = QTime::currentTime();
QString timeString = time.toString("hh:mm:ss");
QString query = QString("insert into timeHold(name,timeVar) values ('ABC','%2')").arg(timeString);
qry->prepare(query);
qry->exec();
但是,我得到 QSqlQuery::value: notlocated on a valid record
.
当我从 SQL Server Management Studio 向表中插入值时,insert into timeHold values('XYZ', '12:17:35')
完美运行.但令我惊讶的是,当我尝试从管理工作室读取存储在表中的值时,我能够获取名称字段,但不能获取时间字段.
When I insert values into the table from SQL Server Management Studio, insert into timeHold values('XYZ', '12:17:35')
works perfectly. To my surprise though, when I have tried reading the values stored in the table from the management studio, I was able to get the name field, but not time field.
这是我用来从表中读取值的代码:
Here is the code I use to read values from the table:
QString query = QString("select * from timeHold");
qry->prepare(query);
qry->exec();
qry->first();
int noOfRecords = qry->numRowsAffected();
do {
qDebug() << qry->value(0).toString();
qDebug() << qry->value(1).toString();
} while (qry->next());
代码产生以下输出:
"ABC"
"\u0017"
"world"
"\u000B"
"Mama"
"\u000B"
"Gerama"
"\u000B"
我怎样才能让它发挥作用?
How can I make it work?
推荐答案
对于保存故事,我会说 arg 方法不起作用,因为您要求 %2 但只有一个元素可以替换
For the saving story , I would say the arg method does not work because you ask for %2 but there is only one element to substitute
query = QString("insert into timeHold(name,timeVar) values ('ABC','%2')").arg(timeString);
我觉得应该是
query = QString("insert into timeHold(name,timeVar) values ('ABC','%1')").arg(timeString);
对于从数据库中检索,要么是因为插入或读取时必须指定格式
For retrieving from the DB, it's either because you have to specify the format when inserting or when reading
这篇关于如何在 SQL Server Express 表中保存 QTime 并读取它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!