problem with std.variant rounding
Nick Sabalausky (Abscissa) via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue May 2 11:16:33 PDT 2017
On 05/02/2017 04:02 AM, Suliman wrote:
>
> I need co concatenate string with variant type (I am doing SQL query).
>
> What is the best way to put it? It's seems that if I am doing simple
> `replace`
>
> string sql = "..."
> sql.replace(`37.72308`, to!string(cargpspoint.lon)).replace(`55.47957`,
> to!string(cargpspoint.lat))
>
> I am loosing accuracy. Is there any better way?
Building SQL strings manually isn't really good practice these days, for
both that and other reasons. It's better to use prepared statements,
which will fix that issue for you and will also ensure your code is not
susceptible to SQL-injection attacks:
// Raw SQL strings (old, ugly, unsafe way):
auto name = "Fred";
auto num = 1.23;
auto sql = text(
"INSERT INTO `myTable` (`field1`, `field2`) VALUES ('",
mysqlEscape(name), "', ", num, ")"
);
exec(conn, sql);
// Prepared statement (good, modern, safe way):
auto name = "Fred";
auto num = 1.23;
Prepared insertSomeFields = prepare(conn,
"INSERT INTO `myTable` (`field1`, `field2`) VALUES (?, ?)"
);
insertSomeFields.setArgs(name, num);
insertSomeFields.exec();
More information about the Digitalmars-d-learn
mailing list