Page 1 of 1

Indexed files

Posted: Tue Mar 12, 2019 1:56 pm
by miskox
Is there a plan to support indexed files? Sometimes it would be easier to use an indexed file instead of a database. For example if I only need one (or relatively small number of files) indexed file. I think that working with the database can also be very hard if you don't have enough experience. Indexed files are very easy to use.

More info could be provided if required. Some info is here:

https://documentation.microfocus.com/he ... _4_7_1_1_3

Other file organizations: https://documentation.microfocus.com/he ... _7_4_7_1_1

There are two ways how to handle indexes:
1. one is to store this information in the .dat file itself
2. the other is to have two separate files: .dat and .idx

Saso

Re: Indexed files

Posted: Tue Mar 12, 2019 3:40 pm
by wilbert
For a small amount of data, you could use a structure with fixed length strings.

Code: Select all

Structure Record
  MembershipNumber.w
  Name.s{20}
EndStructure

Dim Records.Record(99)

With Records(0)
  \MembershipNumber = 1000
  \Name = "John"
EndWith

With Records(99)
  \MembershipNumber = 1100
  \Name = "Luke"
EndWith

If CreateFile(0, "SimpleDatabase.dat")
  WriteData(0, @Records(), 100 * SizeOf(Record))
  CloseFile(0)
EndIf
In this case it's very easy to read or write an entire array.
If you are working with a file instead of an array, it's also easy to seek a record by changing the file pointer.

Re: Indexed files

Posted: Tue Mar 12, 2019 4:12 pm
by TI-994A
miskox wrote:...working with the database can also be very hard...
Quite simple, actually. Here are the component functions, broken down for clarity.

1. Creating an empty SQLite database file (first time only):

Code: Select all

CreateFile(0, GetTemporaryDirectory() + "myDB.sqlite")
CloseFile(0)
2. Preparing the "structure" of the SQLite database (first time only):

Code: Select all

UseSQLiteDatabase()

OpenDatabase(0, GetTemporaryDirectory() + "myDB.sqlite", "", "")
DatabaseUpdate(0, "CREATE TABLE indexFile (name CHAR(200), age INT)")
CloseDatabase(0)
3. Writing data to the SQLite database:

Code: Select all

UseSQLiteDatabase()

OpenDatabase(0, GetTemporaryDirectory() + "myDB.sqlite", "", "")
DatabaseUpdate(0, "INSERT INTO indexFile (name, age) VALUES ('Einstein', '76')")
DatabaseUpdate(0, "INSERT INTO indexFile (name, age) VALUES ('DaVinci', '67')")
DatabaseUpdate(0, "INSERT INTO indexFile (name, age) VALUES ('Newton', '84')")
CloseDatabase(0)
4. Updating data in the SQLite database:

Code: Select all

UseSQLiteDatabase()

OpenDatabase(0, GetTemporaryDirectory() + "myDB.sqlite", "", "")
DatabaseUpdate(0, "UPDATE indexFile SET name='Isaac Newton' WHERE name='Newton'")
CloseDatabase(0)
5. Reading data from the SQLite database:

Code: Select all

UseSQLiteDatabase()

OpenDatabase(0, GetTemporaryDirectory() + "myDB.sqlite", "", "")
DatabaseQuery(0, "SELECT * from indexFile WHERE name='Isaac Newton'")
While NextDatabaseRow(0)
  age = GetDatabaseLong(0, 1)
Wend
Debug "Isaac Newton was " + Str(age) + " years old."
CloseDatabase(0)
That's all that's really required to use an SQLite database!

Re: Indexed files

Posted: Thu Mar 14, 2019 10:52 am
by Cyllceaux
Or the easy-json way

Code: Select all

Structure strDaten
	name.s
	id.d
EndStructure

Structure strD
	List daten.strDaten()
EndStructure

Procedure saveData(file.s,*d.strD)
	Protected json=CreateJSON(#PB_Any)
	InsertJSONStructure(JSONValue(json),*d,strD)
	SaveJSON(json,file)
	FreeJSON(json)
EndProcedure

Procedure loadData(file.s)
	Protected *d.strD=AllocateStructure(strD)
	Protected json=LoadJSON(#PB_Any,file)
	ExtractJSONStructure(JSONValue(json),*d,strD)
	FreeJSON(json)
	ProcedureReturn *d
EndProcedure
Testing

Code: Select all

Protected *d.strD=AllocateStructure(strD)
AddElement(*d\daten())
*d\daten()\id=1
*d\daten()\name="Test 1"

AddElement(*d\daten())
*d\daten()\id=2
*d\daten()\name="Test 2"

saveData("Daten.json",*d)

Protected *d2.strD=loadData("Daten.json")
ForEach *d2\daten()
	Debug *d2\daten()\name
Next