Page 1 of 1

QBasic database .dbf managment?

Posted: Tue Sep 27, 2005 10:33 am
by m4rk0
Hello.
Can someone give me code which will erase rows from database...

For example I have database with 5000 rows, and I want to delete first 3500 of rows (records)?

Any example?

Thank You!

Posted: Tue Sep 27, 2005 11:02 am
by Quibbler
Are the fields delimited or are they fixed length?
Having dealt with .dbf files before I know the headers can also be a big problem - does it have headers?

Posted: Tue Sep 27, 2005 11:15 am
by MystikShadows
Best think I can tell you is to take a look here:

http://qbcm.hybd.net/issues/2-2/

Ethan Winer has a tutorial there that deals with the DBF file format and how to manage it.

Posted: Wed Sep 28, 2005 4:17 am
by Guest
@MystikShadows: Thanx, but there only DB CREATE, DB STRUCT, DB PACK, no DELETE :(

@Quibbler: Hm there is about 10 fields and almost all different. Some are for TEXT only, some for NUMBERS only...

Thanx anyway :)

Posted: Wed Sep 28, 2005 7:21 am
by MystikShadows
ok in order to delete a record in a DBF you need to set it's deleted state. Once that's done, you can use DBPACK to physically delete those records from the database...Sorry forgot to mention that. DBF always worked that way. typically the first byte of that record is the deleted status.

Posted: Wed Sep 28, 2005 10:19 am
by Quibbler
Ok so I've found a program I used to read DBF files. It may give you some ideas. Of course there is no guarantee that all .dbf files follow this format. The header seems to be in 32 byte blocks if the first byte is hex0D then it means "start of data" otherwise the 17th byte gives the length of a field so adding up all the 17th bytes gives the total record length.

Code: Select all

CLS
DIM a AS STRING * 32
DIM b AS STRING * 1
OPEN "r", #1, "c:\data3\datafile.dbf", 32
GET #1, , a
FOR i = 1 TO 5000
GET #1, , a
IF ASC(LEFT$(a, 1)) = &HD THEN 100
j = ASC(MID$(a, 17, 1))
k = k + j
NEXT i
100 CLOSE #1
j = 0
srec = (i * 32) + 2
recl = k + 1
OPEN "b", #1, "c:\data3\datafile.dbf"
FOR i = srec TO 5000
GET #1, i, b
j = j + 1
q$ = q$ + b
IF j = recl THEN
count = count + 1
PRINT q$; " "; count
j = 0
q$ = ""
END IF
NEXT i
CLOSE #1

Posted: Wed Sep 28, 2005 10:46 am
by Antoni
You can get the dbf format description from www.wotsit.org
And yes, to erase a record, just mark it as unused. The pack function copies the dbf in another one skiping the erased fields, then deletes the original.

BTW: Ethan winer dbf library is not portable to FB, as it's based in the FIELD instruction, not implemented in FB