Hello,
I have a little issue with prepared statement. I have a lot of object to insert
a DB, so I want to use a prepared query to lower the preparation cost of the
query. My object are used by soci with a custom type_conversion implementation.
When I write the request directly (ie: sql << "INSERT INTO ... ",
soci::use(mystruct) ), the query works fine. but when I use It with a prepared
statement, values of the used fields aren't updated.
here is an example which reproduce the problem. after executing this program,
values stored in the DB are set to an undefined value (the value of the field
at the preparation moment).
#include <soci/soci.h>
#include <soci/sqlite3/soci-sqlite3.h>
#include <string>
struct MyStruct
{
int v1;
std::string v2;
double v3;
};
namespace soci
{
template <>
class type_conversion<MyStruct>
{
public:
typedef values base_type;
static void from_base(const values& v, indicator /* ind */, MyStruct& p)
{
p.v1 = v.get<int>("V1");
p.v2 = v.get<std::string>("V2");
p.v3 = v.get<double>("V3");
}
static void to_base(const MyStruct& p, values & v, indicator & ind)
{
v.set("V1", p.v1);
v.set("V2", p.v2);
v.set("V3", p.v3);
ind = i_ok;
}
};
}
int main()
{
MyStruct data;
data.v2 = "coincoin";
soci::session sql(soci::sqlite3, "plop.db");
sql << "CREATE TABLE foo (v1 integer, v2 varchar(255), v3 numeric(8,4));";
soci::statement st((sql.prepare << "INSERT INTO foo (v1, v2, v3) VALUES
(:V1, :V2, :V3)", soci::use(data)));
for (int i = 0; i < 5; i++)
{
data.v1 = i;
data.v2 = i * 2.2;
st.execute(true);
}
}
$> sqlite3 ./plop.db
SQLite version 3.6.10
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from foo;
-1077458328|coincoin|-8.24223980946536e-39
-1077458328|coincoin|-8.24223980946536e-39
-1077458328|coincoin|-8.24223980946536e-39
-1077458328|coincoin|-8.24223980946536e-39
-1077458328|coincoin|-8.24223980946536e-39
Thanks,
Pierre
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users