 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
DIKonsult Guest
|
Posted: Tue Jan 17, 2006 9:42 pm Post subject: How to store data types in database? |
|
|
Hi,
try to store a value of my own type in a Paradoxtable and write and read the
value from my Delphi 2005 application..
type
TMytype = (TheOne,TheTwo,TheTree);
var
MyType:TMyType;
begin
Table1.FieldByName('MyType').value:=TheOne;
MyType:=Table1.FieldByName('MyType').value;
end;
This do not work, but I like to know if it's possible to do and how to do it
if possible.
Which datatype to use in the table and how to convert it when using the
value.
Dag
|
|
| Back to top |
|
 |
Andreas Koch Guest
|
Posted: Tue Jan 17, 2006 9:53 pm Post subject: Re: How to store data types in database? |
|
|
DIKonsult wrote:
| Quote: | type
TMytype = (TheOne,TheTwo,TheTree);
Table1.FieldByName('MyType').value:=TheOne;
|
| Quote: | This do not work, but I like to know if it's possible to do and how to do it
if possible.
Which datatype to use in the table and how to convert it when using the
value.
|
I usually write a typ2string and a string2typ function (with string
arrays containing the names of the elements) for better readability.
Or you could use ord(TheOne) to convert it to a number, but i forgot
how to convert it back :-)
|
|
| Back to top |
|
 |
Bob Richardson Guest
|
Posted: Tue Jan 17, 2006 10:46 pm Post subject: Re: How to store data types in database? |
|
|
"Andreas Koch" <nospam (AT) kochandreas (DOT) com> wrote
| Quote: | DIKonsult wrote:
type
TMytype = (TheOne,TheTwo,TheTree);
Table1.FieldByName('MyType').value:=TheOne;
This do not work, but I like to know if it's possible to do and how to do
it
if possible.
Which datatype to use in the table and how to convert it when using the
value.
I usually write a typ2string and a string2typ function (with string
arrays containing the names of the elements) for better readability.
Or you could use ord(TheOne) to convert it to a number, but i forgot
how to convert it back
|
While there's a nice easy way to convert from tmytype to an integer...using
ORD(), I don't believe the converse is true. I've always used a case
statement:
Case value of
0 : MyType := TheOne;
1 : MyType := TheTwo;
etc.
or sometimes I use a loop
MyType := TheOne;
while value <> ord(MyType) do
MyType := SUCC(MyType);
In either case, make sure you handle situations where the value is NOT a
valid ORD(tmyType)
|
|
| Back to top |
|
 |
Maarten Wiltink Guest
|
Posted: Tue Jan 17, 2006 10:57 pm Post subject: Re: How to store data types in database? |
|
|
"DIKonsult" <dag.idoff (AT) di-konsult (DOT) se> wrote
| Quote: | try to store a value of my own type in a Paradoxtable and write and
read the value from my Delphi 2005 application..
type
TMytype = (TheOne,TheTwo,TheTree);
var
MyType:TMyType;
begin
Table1.FieldByName('MyType').value:=TheOne;
MyType:=Table1.FieldByName('MyType').value;
end;
This do not work, but I like to know if it's possible to do and how
to do it if possible.
Which datatype to use in the table and how to convert it when using
the value.
|
Other people have pointed out that an enumeration is almost an integer
and can conveniently be converted to it using Ord(). Converting it back
is best done with a typecast - "best" is a relative here.
I don't know if Paradox supports referential integrity and foreign keys,
but if so, it would be an option to add a domain table and model the
value as a foreign key into it.
Groetjes,
Maarten Wiltink
|
|
| Back to top |
|
 |
pr Guest
|
Posted: Wed Jan 18, 2006 4:11 am Post subject: Re: How to store data types in database? |
|
|
"DIKonsult" <dag.idoff (AT) di-konsult (DOT) se> wrote
| Quote: | Hi,
try to store a value of my own type in a Paradoxtable and write and read
the
value from my Delphi 2005 application..
type
TMytype = (TheOne,TheTwo,TheTree);
var
MyType:TMyType;
begin
Table1.FieldByName('MyType').value:=TheOne;
MyType:=Table1.FieldByName('MyType').value;
end;
This do not work, but I like to know if it's possible to do and how to do
it
if possible.
Which datatype to use in the table and how to convert it when using the
value.
|
Paradox has a limited number of types. You have to convert your
data in the appropriate way so that it matches one of the Paradox
data types.
There is a more serious consideration and that is the use of
Paradox itself, known to mysteriously lose data, prone to
index corruption and troublesome on networked systems, esp.
if there is a mix of XP/NT and Win9x/ME machines.
I think it is mind boggling that someone could even consider
using Paradox when there are excellent alternatives available
for free.
PR
|
|
| Back to top |
|
 |
Delphi Fan Guest
|
Posted: Wed Jan 18, 2006 4:44 pm Post subject: Re: How to store data types in database? |
|
|
Maarten is on the money here - a typecast will do it:
function NumberToMyType(ANumber: integer):TMyType;
begin
Result := TMyType(ANumber); // <--- this is the typecast
end;
Beware of two other things:
1) Enumerated types start from ZERO - you should have
type
TMytype = (TheZero, TheOne,TheTwo,TheThree);
2) Spotted a spelling mistake - its THREE - TREE is a thing with
branches and leaves. ;-)
Hope this helps.
|
|
| Back to top |
|
 |
DIKonsult Guest
|
Posted: Thu Jan 19, 2006 6:26 pm Post subject: Re: How to store data types in database? |
|
|
Hi and thanks for your help.
Below you can see how I use it. If you have any sugestions on improvements
I'll be even happier :-)
Dag
type
TTypStr = string[10];
TCDItyp = (TypNone, TypR, TypP, TypL, TypM, TypT,TypD,TypA);
TTypArray = array [0..7] of TCDItyp;
TTypStrArray = array [0..7] of TTypStr;
TTableArray = array [TypNone..TypA] of TTable;
const
TypArray :TTypArray=(TypNone, TypR, TypP, TypL, TypM, TypT,TypD,TypA);
TypStrArray:TTypStrArray = ('None', 'R','P','L', 'M', 'T','D','A');
implementation
function TypToStr(Typ:TCDItyp):string;
begin
if (ord(typ) >= low(TypStrArray)) and (ord(typ) <= high(TypStrArray))
then
Result:=TypStrArray[ord(Typ)]
else Result:='None';
end;
function StrToTyp(Str:string):TCDItyp;
var
i:integer;
begin
result:=TCDItyp(0);
for i:=low(TypStrArray) to high(TypStrArray) do
if TypStrArray[i] = str then
begin
Result:=TCDItyp(i);
Break;
end;
end;
procedure TFormMain.FormShow(Sender: TObject);
begin
with DatamoduleA do
begin
TableArray[TypNone] := nil;
TableArray[TypR] := TableR;
TableArray[TypP] := TableP;
TableArray[TypL] := TableL;
TableArray[TypM]:= TableM;
TableArray[TypT]:= TableT;
TableArray[TypD]:= TableD;
TableArray[TypA]:= TableA;
end;
end;
procedure Test(CDItyp:TCDItyp);
begin
TableArray[CDItyp].Insert;
TableArray[CDItyp].FieldByName('RecordCode').Value:=TypToStr(CDItyp);
......
......
TableArray[CDItyp].Post;
end;
"DIKonsult"
news:Dxdzf.154160$dP1.512607 (AT) newsc (DOT) telia.net...
| Quote: | Hi,
try to store a value of my own type in a Paradoxtable and write and read
the
value from my Delphi 2005 application..
type
TMytype = (TheOne,TheTwo,TheTree);
var
MyType:TMyType;
begin
Table1.FieldByName('MyType').value:=TheOne;
MyType:=Table1.FieldByName('MyType').value;
end;
This do not work, but I like to know if it's possible to do and how to do
it
if possible.
Which datatype to use in the table and how to convert it when using the
value.
Dag
|
|
|
| Back to top |
|
 |
Hans-Peter Diettrich Guest
|
Posted: Thu Jan 19, 2006 6:49 pm Post subject: Re: How to store data types in database? |
|
|
DIKonsult schrieb:
| Quote: | Below you can see how I use it. If you have any sugestions on improvements
I'll be even happier :-)
Dag
type
TTypStr = string[10];
TCDItyp = (TypNone, TypR, TypP, TypL, TypM, TypT,TypD,TypA);
TTypArray = array [0..7] of TCDItyp;
TTypStrArray = array [0..7] of TTypStr;
TTableArray = array [TypNone..TypA] of TTable;
const
TypArray :TTypArray=(TypNone, TypR, TypP, TypL, TypM, TypT,TypD,TypA);
TypStrArray:TTypStrArray = ('None', 'R','P','L', 'M', 'T','D','A');
|
What's the purpose of TypArray and TTypArray?
When you replace [0..7] by [TCDItyp], also for TTypStrArray and
TTableArray, you'll see that TypArray[x] is equal to x.
| Quote: |
implementation
function TypToStr(Typ:TCDItyp):string;
begin
if (ord(typ) >= low(TypStrArray)) and (ord(typ) <= high(TypStrArray))
then
Result:=TypStrArray[ord(Typ)]
else Result:='None';
end;
|
I'd not type the Typ parameter, it should stay Integer or something
general. Then you'll have not to apply an typecast in a call of
TypToStr, and inside the function also only a single typecast is
required, when TypStrArray is dimensioned as [TCDItyp] as suggested above.
| Quote: |
function StrToTyp(Str:string):TCDItyp;
var
i:integer;
begin
result:=TCDItyp(0);
for i:=low(TypStrArray) to high(TypStrArray) do
if TypStrArray[i] = str then
begin
Result:=TCDItyp(i);
Break;
end;
end;
|
This function can be made simpler and faster, using the first character
of the string as an hash code. Pos(Str[1], 'RPLMTDA') will give the
(only) possible index in the TypStrArray, so that a single compare with
that item will verify this guess. The string of course should become a
named constant, then it's defined near the related declarations, with
less problems when the code must be modified later.
Keep it simple, using the power of Delphi ;-)
DoDi
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|