www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string concatenation with %s

reply "Suliman" <evermind live.ru> writes:
I need to construct complex SQL request, like:

string sql = ("INSERT INTO test.geomagnetic (`date`, 
`a_fredericksburg`, `fredericksburg`, `a_college`, `college`, 
`a_planetary`, `planetary`) VALUES ('%s', '%s', '%s', '%s', '%s', 
'%s', '%s');", date[i], a_fredericksburg[i], fredericksburg[i], 
a_college[i], college[i], a_planetary[i], planetary[i]);


I except that writefln have some behavior as string 
concatenation, but it does not.

IS there any way to put needed values in place of %s in string?
Jan 07 2015
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Suliman:

 I need to construct complex SQL request, like:

 string sql = ("INSERT INTO test.geomagnetic (`date`, 
 `a_fredericksburg`, `fredericksburg`, `a_college`, `college`, 
 `a_planetary`, `planetary`) VALUES ('%s', '%s', '%s', '%s', 
 '%s', '%s', '%s');", date[i], a_fredericksburg[i], 
 fredericksburg[i], a_college[i], college[i], a_planetary[i], 
 planetary[i]);


 I except that writefln have some behavior as string 
 concatenation, but it does not.

 IS there any way to put needed values in place of %s in string?
Please show the _clean_ input, followed by an output example. Bye, bearophile
Jan 07 2015
parent reply "Suliman" <evermind live.ru> writes:
 Please show the _clean_ input, followed by an output example.

 Bye,
 bearophile
to prevent visual corruption I had past it here: http://www.everfall.com/paste/id.php?ftzy9lxr6yfy
Just FYI use prepared statements instead of string concatenation
for SQL queries. You mean some tools, that help co construct request? How to use them with В, The problem is make proper string. Long string become unreadable :(
Jan 07 2015
parent reply "novice2" <sorry noem.ail> writes:
what if a_college[i] will contain ` char?
almost SQL have "prepare" statement...
Jan 07 2015
parent reply "Suliman" <evermind live.ru> writes:
same problem. I am preparing string to next SQL request:

string sss = format("SELECT * FROM test.imgs WHERE src LIKE 
CONCAT('%', REPLACE(CAST(CURDATE()as char), "-", ""), '%') OR 
CONCAT('%', CAST(CURDATE()as char), '%')");

but I am getting next error:

source\app.d(178): Error: invalid array operation "SELECT * FROM 
test.imgs WHERE
  src LIKE CONCAT('%', REPLACE(CAST(CURDATE()as char), " - ", ), 
'%') OR CONCAT('
%', CAST(CURDATE()as char), '%')" (possible missing [])
FAIL 
.dub\build\application-debug-windows-x86-dmd_2067-112ADE4A65EFD822A10EE5558
208F889\ geodataloader executable
Mar 30 2015
parent reply "anonymous" <anonymous example.com> writes:
On Monday, 30 March 2015 at 17:18:01 UTC, Suliman wrote:
 same problem. I am preparing string to next SQL request:

 string sss = format("SELECT * FROM test.imgs WHERE src LIKE 
 CONCAT('%', REPLACE(CAST(CURDATE()as char), "-", ""), '%') OR 
 CONCAT('%', CAST(CURDATE()as char), '%')");
Here's your code without SQL noise: string sss = format("foo"-", ""bar"); It should be obvious now that you forgot to escape those double quotes.
Mar 30 2015
parent reply "Suliman" <evermind live.ru> writes:
 string sss = format("foo"-", ""bar");

 It should be obvious now that you forgot to escape those double 
 quotes.
Thanks! Is there any way to stay string as is. without need of it's escaping and so on? It's seems I have seen something like it in docs, but I am not sure about it...
Mar 30 2015
parent "anonymous" <anonymous example.com> writes:
On Monday, 30 March 2015 at 17:34:20 UTC, Suliman wrote:
 string sss = format("foo"-", ""bar");

 It should be obvious now that you forgot to escape those 
 double quotes.
Thanks! Is there any way to stay string as is. without need of it's escaping and so on? It's seems I have seen something like it in docs, but I am not sure about it...
There are various other string literal forms [1] where you don't need to escape double quotes: AlternateWysiwygString - backticks as delimiters: `foo "-", "" bar` DelimitedString - can use delimiters of choice, here parentheses: q"(foo "-", "" bar)" TokenString - for D code, probably not a good choice here, but works: q{foo "-", "" bar} Of course, as they're all delimiter based, there will always something you can't put into the string verbatim. [1] http://dlang.org/lex.html#StringLiteral
Mar 30 2015
prev sibling next sibling parent reply Justin Whear <justin economicmodeling.com> writes:
On Wed, 07 Jan 2015 16:38:23 +0000, Suliman wrote:

 I except that writefln have some behavior as string concatenation, but
 it does not.
 
 IS there any way to put needed values in place of %s in string?
std.string.format interpolates string with the same behavior as writefln but returns the resulting string instead of printing it.
Jan 07 2015
parent "Suliman" <evermind live.ru> writes:
std.string.format interpolates string with the same behavior as 
writefln
Thanks!
Jan 07 2015
prev sibling parent "Gary Willoughby" <dev nomad.so> writes:
On Wednesday, 7 January 2015 at 16:38:25 UTC, Suliman wrote:
 I need to construct complex SQL request, like:

 string sql = ("INSERT INTO test.geomagnetic (`date`, 
 `a_fredericksburg`, `fredericksburg`, `a_college`, `college`, 
 `a_planetary`, `planetary`) VALUES ('%s', '%s', '%s', '%s', 
 '%s', '%s', '%s');", date[i], a_fredericksburg[i], 
 fredericksburg[i], a_college[i], college[i], a_planetary[i], 
 planetary[i]);


 I except that writefln have some behavior as string 
 concatenation, but it does not.

 IS there any way to put needed values in place of %s in string?
Just FYI use prepared statements instead of string concatenation for SQL queries. http://en.wikipedia.org/wiki/Prepared_statement
Jan 07 2015