Indexed files

Everything else that doesn't fall into one of the other PB categories.
miskox
User
User
Posts: 95
Joined: Sun Aug 27, 2017 7:37 pm
Location: Slovenia

Indexed files

Post 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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Indexed files

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Indexed files

Post 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!
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Cyllceaux
Enthusiast
Enthusiast
Posts: 458
Joined: Mon Jun 23, 2014 1:18 pm
Contact:

Re: Indexed files

Post 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
Post Reply