Announcement

Collapse
No announcement yet.

Finding a specific record in Random Access Files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Finding a specific record in Random Access Files

    Dear Members:
    I am a newbie and still trying to learn. The question I have is it says in the Help File that you can access any record in a Random Access File but doesn't tell me how to do it. In the olden days I used to write in dBASE IV and you could use a SEEK(variable) to find a specific variable in a field in the database. Can someone tell me how to find a specific variable without having to read each record in the file AND how to get the RECORD NUMBER of that record. I have searched the Support Community and can't find an answer. Also can Random Access Files be indexed and if so how. I read where members refer to indexing a sequential file but I DON'T KNOW HOW TO DO THAT EITHER. The file I will be creating will be rather small maybe less than 2000 records so which type should I use Sequential or Random. I was going to use Random because I will need to find a specific variable in a record. Now I can find this specific variable by reading each record in the file but how do you go directly to the record containing the variable in a RANDOM ACCESS FILE.

    Thanks all help will be very much appreciated
    Leroy Hindman

  • #2
    This is one way.
    Another way is to read the entire file into an array then use the elements as records.
    Another way is PowerBASIC has a field statement with an example in the help file.
    Code:
    #DIM ALL
    
    TYPE MyRecord
     lastname  AS STRING * 30
     firstname AS STRING * 30
    END TYPE
    
    FUNCTION PBMAIN () AS LONG
    
     LOCAL hFile  AS LONG
     LOCAL recnum AS LONG
     LOCAL rec    AS MyRecord
    
     hFile = FREEFILE 'this was missing   1:39PM
     OPEN "test.tmp" FOR RANDOM AS #hFile LEN = LEN(MyRecord)
     IF ERR THEN ? ERROR$,%MB_SYSTEMMODAL,"OPEN":EXIT FUNCTION
    
     recnum = 1
     rec.lastname = "Hindman
     rec.firstname= "Leroy
     PUT #hFile,recnum,rec
     IF ERR THEN ? ERROR$,%MB_SYSTEMMODAL,"PUT":CLOSE #hFile:EXIT FUNCTION
    
     GET #hFile,recnum,rec
     IF ERR THEN ? ERROR$,%MB_SYSTEMMODAL,"GET":CLOSE #hFile:EXIT FUNCTION
    
     ? RTRIM$(rec.lastname) + ", " + RTRIM$(rec.firstname)
    
     CLOSE #hFile
    https://duckduckgo.com instead of google

    Comment


    • #3
      Leroy

      Why is your file a Random Access file?

      In all my programming, I have usually replaced files with arrays. IE at the start of the program, read in the file to an array (or arrays), do my processing on the array (including adding, deleting and amendment) and then write out the array at the end. It is easy to do, it is fast, and PB arrays can be incredibly huge.
      [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
      Kerry Farmer

      Comment


      • #4
        Hi Leroy,

        If you are familiar with Dbase files, with Power Basic you could write easily your own application to handle them. Also, you can find many examples over here.

        Comment


        • #5
          Leroy, also you can use the PowerTree for index records from a Random File. This is a great tool from PowerBasic.

          Comment


          • #6
            Leroy,

            I too use Random Access files to store my data and, has been indicated by Kerry, read the 'index' field data elements into an array at the start of the program. I then use the Array Scan feature in PB to search for a value in the array, which I find to be incredibly quick, especially for relatively small files which is what you have indicated. Just a thought.

            Comment


            • #7
              Thanks Everyone
              So what you guys are saying is there is NO WAY to go directly to a record with a field that matches a variable. If you want to go directly to a record number then you can do that if you know the record number you want (I think I got that right).

              Derick:
              I read the Array Scan feature and it seems that would be my best option. With that said why would a Random Access File be any better than a Sequential File.

              Comment


              • #8
                The Pros and cons of Random Access files are examined in PBHelp topic 'Random Access Files'

                What's best is subjective - depends on the nature of the data that is stored on disk and the needs of the app that will be accessing it.

                Check out the Address sample in PBWin10\samples\DDT\Address\ for some ideas (eg the 'Find' feature)
                Last edited by Dave Biggs; 10 Jan 2019, 06:58 PM. Reason: Add reference to PB sample Address.bas
                Rgds, Dave

                Comment


                • #9
                  > If you want to go directly to a record number then you can do that if you know the record number you want (I think I got that right).
                  Yes, see thread #2
                  DIM record AS RecordType
                  GET #hFile,recnum,record
                  PUT #hFile,recnum,record

                  Random access files allow getting data for a record with a single access and all columns are available by using the field variable or the offset into the record.
                  Only the record to be updated is updated without rewriting the entire file.
                  If used on a network the record can be locked, updated and unlocked.
                  PowerTree or some other index manager might make it easier to maintain, but you can keep your own sorted indexes in memory or on disk.
                  If you search on a column a lot then that column can be kept in a sorted array for faster access to get the record using the elements found.

                  There are also unsupported products that work very well:
                  Cheetah by Paul Squires uses dbase file format and Tsunami by Timm Motl that use btree indexing of variable length records.
                  There may also be other Btree indexing routines available by Chris Boss.
                  I use SQLite/SQLitening for about everything which make searching on anything very easy using SQL select statements.

                  This might be a great program for you to look at:
                  gbTextDB by Gary Beene https://forum.powerbasic.com/forum/u...atabase-engine
                  https://duckduckgo.com instead of google

                  Comment

                  Working...
                  X