wrong field casting

Forum related to SQLite

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
ORMADA
Fresh Boarder
Fresh Boarder
Posts: 12
Joined: 13.02.2006, 12:42

wrong field casting

Post by ORMADA »

hi!
so.. exist next table

CREATE TABLE [operations] (
[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[_Date] DATE NOT NULL,
[_Count] REAL NOT NULL,
);

i use next query

SELECT t2.ID as ID
,t1.Name as Name
,sum((CASE t1.Credit
WHEN 1 THEN t2._Count
ELSE 0
END)) AS credit
,(CASE t1.Credit
WHEN 0 THEN t2._Count
ELSE 0
END) AS debit
,t2.Note as Note
FROM groups t1, operations t2
WHERE t1.ID=t2.groups_ID
AND t2._date = "2007-05-03"
GROUP BY t2.ID,t1.Name
ORDER BY t2.ID;

after query is open we have
credit TStringField... but must be TFloatField
i try to use CAST function but dosent matter TStringField
and because return as string delimiter is '.' but if field is float my delimite is ','(country settings)
xtra
Fresh Boarder
Fresh Boarder
Posts: 7
Joined: 23.03.2007, 16:45

Post by xtra »

The problem you describe relies on the fact that SQLite does not report a fieldtype when you do "SELECT ... AS".
Zeos defaults to use stString as fieldtype when it encounters an unknown fieldtype (in this case it is simply an empty string).

There is no way of knowing the fieldtype without parsing the returned data, which is impossible at the level where the fieldtypes are determined (prior to fetching the data).

So you have to do the conversion on your own, you could do a conversion via:
if DecimalSeparator<>'.' then
StringReplace(Value, '.', DecimalSeparator, [rfReplaceAll])];

which should work on all systems (even ones that use something entirely different as . and , for decimal seperator).

Hope this helps.
dnprossi
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 12.04.2007, 13:23

Post by dnprossi »

hi...

Read your answer but something strange is happening...

Why is it that eg.

ZReadOnlyQuery3.SQL.Add('Select Max(inscDiam) As MaxDiam From objects Where ObjectID = 3');

Returns TStringField

And

ZReadOnlyQuery1.SQL.Add(Memo1.Text);

with

Memo1.Text beeing

Select Max(inscDiam) As MaxDiam From objects Where ObjectID = 3

Same as previous sql statement WORKS returning correct FLOAT????

Thank you!!!
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Hi,
That's strange indeed...
Can you check, eg by using the TZsqlmonitor that the queries sent to the server are binary equal as well?
There may be a closing linefeed (or something similar) that's there when you add using a Memo and not when using Add('...'). It shouldn't make a difference, but ...
dnprossi
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 12.04.2007, 13:23

Post by dnprossi »

Hi,
I tried using TZsqlmonitor as requested :

These are the two queries...

TMEMO
2007-05-22 11.39.41 cat: Execute, proto: sqlite-3, msg: Select Max(inscRadius) As tfMaxRadius From objectsdata Where ObjectID = 1


ADD(...)
2007-05-22 11.39.50 cat: Execute, proto: sqlite-3, msg: Select Max(inscRadius) As tfMaxRadius From objectsdata Where ObjectID = 1

Return Values:

TMEMO
112.498442747681

ADD(...)
112498442747681

Floating Point is lost....

I parsed the lines looking for hidden chars but no difference at this level... Perhaps ZSQLMonitor Cleans it up...

That's strange indeed...???? Yes!!!!
xtra
Fresh Boarder
Fresh Boarder
Posts: 7
Joined: 23.03.2007, 16:45

Post by xtra »

According to the code (ZDbcSqLiteResultSet.pas) it seems impossible to make a difference...
which SQLite.dll do you use?
dnprossi
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 12.04.2007, 13:23

Post by dnprossi »

I am using sqlite 3.3.15 - Zeos 6.6.1 beta - Delphi 7
dnprossi
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 12.04.2007, 13:23

Post by dnprossi »

According to the code (ZDbcSqLiteResultSet.pas) it seems impossible to make a difference...
which SQLite.dll do you use?
Yes... True... Both Tmemo and ADD now do the same thing I was doing something wrong... Now I get both wrong...

After further testing I figured out that SQLite Float is returned with Decimal Separator "." instead of "," as with MySql...

I created two small exactly same databases with sqllite and mysql...

I open one at a time with exactly same source...

MySql:
ShowMessage(ZReadOnlyQuery1['tfMax']);
Returns
112,498442747681 Recognized as Float...

SQLite:
ShowMessage(ZReadOnlyQuery1['tfMax']);
Returns
112.498442747681 Not Recognized...



procedure TForm1.Button1Click(Sender: TObject);
var
tfMaxRad: Double;
newStr: String;
P1: Integer;
begin
ZReadOnlyQuery1.SQL.Clear;
ZReadOnlyQuery1.SQL.Add(Memo1.Text);
ZReadOnlyQuery1.ExecSQL;
ZReadOnlyQuery1.Active := True;
ShowMessage(ZReadOnlyQuery1['tfMax']);
P1 := Pos('.', ZReadOnlyQuery1['tfMax']);
if P1 > 0 then
begin
newStr := ZReadOnlyQuery1['tfMax'];
Delete(newStr, P1, 1);
Insert(',', newStr, P1);
tfMaxRad := StrToFloat(newStr);
end
else
tfMaxRad := ZReadOnlyQuery1['tfMax'];
ShowMessage(FloatToStr(tfMaxRad));
end;
dnprossi
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: 12.04.2007, 13:23

Post by dnprossi »

Sorry!! Last piece of code is what I used to solve problem...

In ZDbcSqLiteResultSet.pas
TZSQLiteResultSet.Open
TypeName Always Nill...
aducom
Zeos Dev Team
Zeos Dev Team
Posts: 67
Joined: 30.08.2005, 13:21

Post by aducom »

It is odd, since SQLite does know the fieldtype because it is depending on it's expression. Ormada, could you send a small failing sample to aducom at support dot com, I'll like to look into this deeper. Please keep out exe as you probabely already know ;-)

albert
Post Reply