Overview
Features
Download
Documentation
Community
Add-Ons & Services

Inserting row with NULL values into a database

Please post support and help requests here.

Inserting row with NULL values into a database

Postby mkaut » 17 Oct 2012, 15:31

Hello,

I want to insert a table into a database. Looking at the documentation, the easiest think seems to be to build a vector of tuples and then insert it into the database.
However, some of the values might be unknown, so I wonder if there is a way to represent this using this approach. In other words, can I use vector of tuples to insert NULL values in the database? (One natural candidate would be boost::optional, but I have not seen it mentioned anywhere?)

And if there is no natural way of doing this using tuples, can one do it using the "complex data type mapping" mentioned in the POCO Data user guide?

Thanks.

Michal
mkaut
 
Posts: 19
Joined: 17 Oct 2012, 15:22

Re: Inserting row with NULL values into a database

Postby alex » 17 Oct 2012, 20:18

mkaut wrote:However, some of the values might be unknown, so I wonder if there is a way to represent this using this approach. In other words, can I use vector of tuples to insert NULL values in the database? (One natural candidate would be boost::optional, but I have not seen it mentioned anywhere?)

Use Poco::Tuple and Poco::Nullable
mkaut wrote:And if there is no natural way of doing this using tuples, can one do it using the "complex data type mapping" mentioned in the POCO Data user guide?

Yes, here's a (1.5.0) example:
Code: Select all
typedef Tuple<Nullable<int>, Nullable<double>, Nullable<std::string>, Nullable<DateTime> > TupleType;
TupleType t;
ses << "INSERT INTO NullableTest VALUES (?, ?, ?, ?)", use(t), now;
TupleType ret(1, 1.5, std::string("abc"), DateTime());
assert (ret != t);
ses << "SELECT * FROM NullableTest", into(ret), now;
assert (ret.get<0>().isNull());
assert (ret.get<1>().isNull());
assert (ret.get<2>().isNull());
assert (ret.get<3>().isNull());
alex
 
Posts: 1117
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Inserting row with NULL values into a database

Postby mkaut » 18 Oct 2012, 08:30

Hello Alex,

thanks for the quick answer. This is exactly the piece I was missing :-)
mkaut
 
Posts: 19
Joined: 17 Oct 2012, 15:22


Return to Support

Who is online

Users browsing this forum: gregee123 and 3 guests