Board index » cppbuilder » Using text file as a database ?!!

Using text file as a database ?!!


2003-07-31 07:44:01 PM
cppbuilder37
Hi All,
I want to store data I collect using my application but I don't need to use
a database driver, so I was thinking of using a text file instead.
But I didn't deal with text files before (working with it as if I'm working
with a database file) I need to perform save(insertion), update, delete and
search stored data
I need to know if it is possible and if so how should I start and how would
I get a resource for this subject
thanks
 
 

Re:Using text file as a database ?!!

T.Walker wrote:
Quote
I want to store data I collect using my application but I don't need to use
a database driver,
You think you don't need a database. A driver is something you can
use to talk with the database. What is the reason ? Not to much
data, not to much tables, not to much records, not to much relations ?
Could you give an indication ?
Quote
so I was thinking of using a text file instead.
That is very well possible.
Quote
But I didn't deal with text files before (working with it as if I'm working
with a database file) I need to perform save(insertion), update, delete and
search stored data
If you take a TStringList then the STringList could load the whole
textfile in memory. You could use a StringList for every table.
A record in the table would be a line in the txtfile and a String
in the StringList.
The StringList provides Add(), Insert(), Sorted/Unsorted, a CustomSort
(bcb5 up), SaveToFile() and LoadFromFile().
Quote
I need to know if it is possible and if so how should I start and how would
I get a resource for this subject
What do you mean with a resource ?
Hans.
 

Re:Using text file as a database ?!!

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
If you take a TStringList then the STringList could load the
whole textfile in memory. [...]
Here we go with StringList again !!! Actually, it's a nice
solution for a flat file but how would you propose to deal
with a flat file of structures with text and ints and doubles?
~ JD
 

{smallsort}

Re:Using text file as a database ?!!

JD wrote:
Quote
Here we go with StringList again !!!
!!!!
Quote
Actually, it's a nice
solution for a flat file but how would you propose to deal
with a flat file of structures with text and ints and doubles?
Well T.Walker wants it in text. So if you have (Database)Fields
which are int, double and/or text than all would be stored
as text. The fields can be separated with komma's. This is
widely done with csv: comma separated value. A structure
would be a record on its own.
Hans.
 

Re:Using text file as a database ?!!

I'm having a very similler problem here
I'm working on a small application so I don't want to install a database
driver with my app.
So I was thinking of something like using a text file but some says that
using XML is much much better
and I still didn't make my mind which of them is better
and I haven't work with any of both before
I'm working on BCB 5 ent.
thanks in advance
"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
JD wrote:

>Here we go with StringList again !!!

!!!!

>Actually, it's a nice
>solution for a flat file but how would you propose to deal
>with a flat file of structures with text and ints and doubles?

Well T.Walker wants it in text. So if you have (Database)Fields
which are int, double and/or text than all would be stored
as text. The fields can be separated with komma's. This is
widely done with csv: comma separated value. A structure
would be a record on its own.

Hans.

 

Re:Using text file as a database ?!!

DRE wrote:
Quote
So I was thinking of something like using a text file but some says that
using XML is much much better
An xml file in a textfile.
If you would use csv then the file would look for instance
name,age,bcbversion
Jack,23,3
Martin,44,5
And in xml:
<record>
<name>Jack</name>
<age>23</age>
<bcbversion>3</bcbversion>
</record>
<record>
<name>
Martin
</name>
<age>44</age>
<bcbversion>5</bcbversion>
</record>
xml is in, and very handy. It takes much (file)space.
It is not difficult to make functions like
AnsiString get_record ( int recnr );
AnsiString get_item ( recnr, "age" );
Quote
and I still didn't make my mind which of them is better
I do not like to speak in terms of better.
You should only look which fits you best.
TStringList is handy for csv as every record is a String.
Using xml you have more overhead I think. But once you wrote
the basic insert and retrievel functions it's ok.
Hans.
 

Re:Using text file as a database ?!!

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
[...] Using xml you have more overhead I think. But once you
wrote the basic insert and retrievel functions it's ok.
You forgot to mention that many other applications can work
with XML as well. So .... if you *might* want to migrate the
data to a new application in the future, XML may be the way to
go.
~ JD
 

Re:Using text file as a database ?!!

When xml is used only waisted space is used in the text file when loaded you
would write routines that converted the xml file into a structure ???
Just as you would with the coma sep version ???
both would handle dynamic strings well ???
The xml version would only be overhead heavy in the case of large records
with many items ???
Also isn't there built in function that can be used in cbuilder or microsoft
to work with xml as with csv ???
"Hans Galema" < XXXX@XXXXX.COM >wrote in message
Quote
DRE wrote:

>So I was thinking of something like using a text file but some says that
>using XML is much much better

An xml file in a textfile.

If you would use csv then the file would look for instance
name,age,bcbversion
Jack,23,3
Martin,44,5

And in xml:
<record>
<name>Jack</name>
<age>23</age>
<bcbversion>3</bcbversion>
</record>
<record>
<name>
Martin
</name>
<age>44</age>
<bcbversion>5</bcbversion>
</record>

xml is in, and very handy. It takes much (file)space.

It is not difficult to make functions like

AnsiString get_record ( int recnr );
AnsiString get_item ( recnr, "age" );

>and I still didn't make my mind which of them is better

I do not like to speak in terms of better.

You should only look which fits you best.

TStringList is handy for csv as every record is a String.

Using xml you have more overhead I think. But once you wrote
the basic insert and retrievel functions it's ok.

Hans.

 

Re:Using text file as a database ?!!

"Joe Vasher" < XXXX@XXXXX.COM >wrote:
Quote
When xml is used only waisted space is used in the text file
when loaded you would write routines that converted the xml
file into a structure ???
Of course both files would need parsing and both files are
text files so if there exists a numeric field, that field,
once parsed, would also need to be converted to an int or
double ect.
Quote
Just as you would with the coma sep version ???
The parsing would be different but since it's all text, once
parsed, putting the data into a structure would be the same.
However, a comma seperated file *seems* to be more easily
parsed (I haven't parsed XML):
StringList1->LoadFromFile( "SomeFile" );
StringList2->CommaText( StringList1->Strings[ RecordIndex ] );
Now list2 contains all of the fields for the RecordIndex
record parsed into individual strings. You can loop through
them and set the values for the desired structure.
Quote
both would handle dynamic strings well ???
Sure, as long as you do it correctly.
Quote
The xml version would only be overhead heavy in the case of
large records with many items ???
Incorrect. XML generates much larger files although there is a
little bit more overhead to produce the XML file.
Quote
Also isn't there built in function that can be used in
cbuilder or microsoft to work with xml as with csv ???
Comma delimited files first showed up (for me) sometime around
DOS 3.0 with Lotus. It's old stuff and I doubt if it will be
depreciated any time soon because of it's simplicity. Many
programs accept this format for importing data.
XML is much more current and while some programs also accept
this format for importing, it's more widely used with SDK's
and COM objects to transfere data between programs at runtime.
~ JD
 

Re:Using text file as a database ?!!

JD wrote:
Quote
StringList1->LoadFromFile( "SomeFile" );
StringList2->CommaText( StringList1->Strings[ RecordIndex ] );

Now list2 contains all of the fields for the RecordIndex
record parsed into individual strings.
You have to check the case where textfields can contain comma's too.
In csv a comma in a text or floatreprsentation (as to differentiate
from a separator) is escaped.
"Hey Joe\, how are you?"
How would you handle that in a TStringList ?
Hans.
 

Re:Using text file as a database ?!!

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote

"Hey Joe\, how are you?"

How would you handle that in a TStringList ?
When I created the string to be written to a file, I would have just removed the slash but kept the quotes. CommaText would then return:
Hey Joe, how are you?
~ JD
 

Re:Using text file as a database ?!!

JD wrote:
Quote
When I created the string to be written to a file, I would have just removed the slash but kept the quotes. CommaText would then return:

Hey Joe, how are you?
Please wrap your lines.
Oh, so when commas are between quotes, then they are neglected by CommaText ?
That's even better.
Hans.
 

Re:Using text file as a database ?!!

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote

Please wrap your lines.

I was watching tv with a friend the other night and I had the
wrong {*word*190}er. In my defense I said to her "Hey! I don't walk
on water you know.". She replied "No. You just know where the
stones are.". Some one must have moved one of my stones ;-)
Quote
Oh, so when commas are between quotes, then they are
neglected by CommaText ?
Correct. However, there are a number of other dilemiters that CommaText acts on - including spaces. So, it's simply smart to
add the quotes whether you need them or not. And if you desire
double quotes in the text then use double double quotes:
""Hey Joe, how are you?""
~ JD
P.S. If you want to use the quote, credit B. Huglin 2003
 

Re:Using text file as a database ?!!

Hans Galema < XXXX@XXXXX.COM >wrote:
Quote
JD wrote:

>""Hey Joe, how are you?""

>P.S. If you want to use the quote, credit B. Huglin 2003

Tell me more please.
LOL. I was refering to "I don't walk on water. I just know
where the stones are.".
If you want to know more about how CommaText parses, google for
'SDF formatted text'.
~ JD
 

Re:Using text file as a database ?!!

JD wrote:
Quote
If you want to know more about how CommaText parses, google for
'SDF formatted text'.
Thanks.
Hans.