You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I see that if the timestamp column of the record is "NULL", then I got the result that every members of the std::tm is 0.
I think this is correct, because I have such line row.timestamp = v.get<std::tm>("timestamp", std::tm());, and the last argument std::tm() is the default value if the row has the NULL for this field.
But when I try to run the to_base function, I got an error if I have such code:
MyClass obj;
obj.timestamp = std::tm();
// later I try to add the obj to the database
The error says that: (I strip the error message, because the actual class name is not "MyClass")
can't find the MyClass with id = 8, Incorrect datetime value: '1900-01-00 00:00:00' for column 'timestamp' ......
So, it looks like I can't convert/write an empty std::tm object which as all the members as 0s 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 the timestamp filed as a NULL.
So, some code may be changed to
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 (row.timestamp == std::tm())
{
v.set("timestamp", soci::i_null);
}
else
{
v.set("timestamp", row.timestamp);
}
ind = i_ok;
}
Am I correct?
Thanks.
The text was updated successfully, but these errors were encountered:
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.
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.
The text was updated successfully, but these errors were encountered: