It is currently Mon May 20, 2019 6:35 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Indexed files
PostPosted: Tue Mar 12, 2019 1:56 pm 
Offline
User
User

Joined: Sun Aug 27, 2017 7:37 pm
Posts: 29
Location: Slovenia
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Indexed files
PostPosted: Tue Mar 12, 2019 3:40 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3339
Location: Netherlands
For a small amount of data, you could use a structure with fixed length strings.
Code:
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.

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Indexed files
PostPosted: Tue Mar 12, 2019 4:12 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2011 3:47 am
Posts: 2211
Location: Singapore
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:
CreateFile(0, GetTemporaryDirectory() + "myDB.sqlite")
CloseFile(0)

2. Preparing the "structure" of the SQLite database (first time only):
Code:
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:
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:
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:
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: Indexed files
PostPosted: Thu Mar 14, 2019 10:52 am 
Offline
User
User

Joined: Mon Jun 23, 2014 1:18 pm
Posts: 89
Or the easy-json way

Code:
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:
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


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

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye