Board index » delphi » Re: Insert asc(0) characters in strings

Re: Insert asc(0) characters in strings


2006-03-26 10:52:53 PM
delphi33
"Mikael Lenfors" <XXXX@XXXXX.COM>wrote
Quote
We have data strings containing all 256 ascii characters
(from 0-255). When I Try to insert them into the database
it fails.
I'm replacing ' characters with two ', this seems to work.
Mikael, What do you mean by "characters with two"?
Quote
The problem seems to be all characters with ascii value 0.
I am not surprised. You probably know that ASCII 0
(also known as #0) is used as a string terminator in
a lot of code.
Quote
How can I insert them? My insert looks like:
ADOQuery.SQL.Add(
'Insert Into RadarData (DataString) Values (' +
StringReplace(MyData,'''','''''',[rfReplaceAll]) + ')' );
DataString is declared as Text and
MyData can be 100K big!
First, I suggest breaking that statement into three statements,
so that you can see the contents of the intermediate strings.
s := StringReplace(MyData,'''','''''',[rfReplaceAll]);
s := 'Insert Into RadarData (DataString) Values (' + s + ')';
ADOQuery.SQL.Add(s);
I suspect that IDE's de{*word*81} may not properly show beyond
the #0 characters, so you could also check the value of
length(s);
Good luck hunting, JohnH
 
 

Re: Insert asc(0) characters in strings

Mikael Lenfors schreef:
Quote
I'm replacing ' characters with two ', this seems to work.
The SysUtils function "QuotedStr" is available to handle this.
Quote
The problem seems
to be all characters with ascii value 0. How can I insert them?
You are inserting binary data into a char type field, asking for problems.
You could instead:
- "escape" each $00 character by replacing it with something else (at
the cost of some extra processing). You can use any string that can not
occur as a substring in you actual data, for instance '#$00' or '0x00'
or '&Null;' or whatever.
- convert all your char data to base64 (at the cost of some extra
processing and space) so you end up with true char data
- use a database blob field type instead of a char field type
Danny
---