Open asmwarrior opened 6 months ago
It looks like I can't compare the if (row.timestamp == std::tm())
, so I change the code like below:
bool IsTmEmpty(const std::tm& time) {
return (time.tm_year == 0 &&
time.tm_mon == 0 &&
time.tm_mday == 0 &&
time.tm_hour == 0 &&
time.tm_min == 0 &&
time.tm_sec == 0);
}
And later, I use such code:
static void to_base(const MyClass& row, values& v, indicator& ind)
{
v.set("id", row.id);
// check to see whether the row.timestamp is an empty null "std::tm", if true, set the field value as NULL
if ( IsTmEmpty(row.timestamp) )
{
v.set("timestamp", soci::i_null);
}
else
{
v.set("timestamp", row.timestamp);
}
ind = i_ok;
}
But sadly, the above code still can't be built. The issue happens here: v.set("timestamp", soci::i_null);
, I'm currently don't know how to use the null indicator.
Thanks.
OK, I think I found the solution, it is inside the document:
See here:
So, the final code which works is like below:
static void to_base(const MyClass& row, values& v, indicator& ind)
{
v.set("id", row.id);
// check to see whether the row.timestamp is an empty null "std::tm", if true, set the field value as NULL
v.set("timestamp", row.timestamp, IsTmEmpty(row.timestamp) ? i_null : i_ok);
ind = i_ok;
}
Hi, I have a MySQL database and I use soci library to access it.
There is a table, which has a "timestamp" column. In the associated C++ user defined class, I have a
std::tm
for this column.Here is a demo code I used to convert the soci row to the user define class and convert back.
I see that if the
timestamp
column of the record is "NULL", then I got the result that every members of thestd::tm
is0
.I think this is correct, because I have such line
row.timestamp = v.get<std::tm>("timestamp", std::tm());
, and the last argumentstd::tm()
is the default value if the row has theNULL
for this field.But when I try to run the
to_base
function, I got an error if I have such code:The error says that: (I strip the error message, because the actual class name is not "MyClass")
So, it looks like I can't convert/write an empty
std::tm
object which as all the members as0
s to the database.My question is how to solve such issue?
My guess is that I see the default value of the
std::tm
object(all members are zero), I would set thetimestamp
filed as a NULL.So, some code may be changed to
Am I correct?
Thanks.