forum.vmspython.org Forum Index forum.vmspython.org
Forum system
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

openVMS database file read and update specific record

 
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS
View previous topic :: View next topic  
Author Message
jbbiuk



Joined: 30 Mar 2016
Posts: 1

PostPosted: Wed Mar 30, 2016 8:53 pm    Post subject: openVMS database file read and update specific record Reply with quote

Hi,

i am new to openvms and trying to make python script that can fetch, update, delete fields in database file.

So far i made it to fetching all the records from database file, if i put "key = 0".
When i try to use string value from the record db, i don't get anything.

Code:

from vms.rms.IndexedFile import IndexedFile
from construct import *
import struct

RecordDB = Struct("RecordDB",
                  String('LINE', 4, padchar=' '),   
                  String('NO', 2, padchar=' '),      
                  String('FIELD', 10, padchar=' '),   
                  String('NAME', 6, padchar=' '),   
                  String('SUR_NAME', 30, padchar=' ')
                 
                  )


class SIMPLE(IndexedFile):
    name = 'simple.dbs'
    def __init__(self):
        IndexedFile.__init__(self, self.name, RecordDB)
    def pack_key(self, keynum, keyval):
        if keynum == 0:
            return struct.pack("=4s", *keyval)
        else:
           raise KeyError
    def keyval(self, keynum):
        if keynum == 0:
            return (self.LINE, self.LINE)
        else:
           raise KeyError
    def primary_keynum(self):
        return 0

f = SIMPLE()
key = "  25"

i = 0
for r in f.fetchall(0,key):
    i = i + 1
    print "----------------------------------------- %s -----------------------------------" % i
    print r.LINE,r.NO,r.FIELD,r.NAME,r.SUR_NAME




and i was wondering what would be best way to fetch one record, and update it or delete, what am i doing wrong here?

Any questions,suggestions, comments or helpful hints are welcome.

Best regards,
JB
Back to top
View user's profile Send private message
jfp



Joined: 12 Jul 2004
Posts: 618

PostPosted: Mon Apr 11, 2016 3:37 pm    Post subject: Reply with quote

Hi,

I have try the following example
File FDL
Code:

SYSTEM
        SOURCE                  "OpenVMS"

FILE
        NAME                    "master001.dbs"
        ORGANIZATION            indexed
        GLBUFF_FLAGS_V83        none

RECORD
        CARRIAGE_CONTROL        none
        FORMAT                  fixed
        SIZE                    22

KEY 0
        CHANGES                 no
        SEG0_LENGTH             10
        SEG0_POSITION           6
        TYPE                    string

KEY 1
        SEG0_LENGTH             6
        SEG0_POSITION           16
        TYPE                    string


data
Code:

F01AAAF003AAAAA1XXXXA1
F01BABF003AAAAB1XXXXA2
F01CACF003AAAAC1XXXXA3
F01DADF003AAAAD1XXXXA4


Script
Code:

import struct
from vms.rms.IndexedFile import IndexedFile
from construct import *


RecordMASTER_1 = Struct("RecordMASTER_1",
    String('F01L001', 4),
String('F01L002', 2),
String('F01L003', 10),
String('F01L004', 6)
)


class TestFile(IndexedFile):
    Name = 'master001.dbs'
    def __init__(self):
        IndexedFile.__init__(self, TestFile.Name, RecordMASTER_1)
    def primary_keynum(self):
        return 0
    def keyval(self, rec, keynum):
        if keynum == 0:
            return rec.F01L003
        elif keynum == 1:
            return rec.F01L004
        else:
            raise "Invalid keynum parameter"
    def pack_key(self, keynum, keyval):
        if keynum == 0:
            return struct.pack("=10s", keyval)
        elif keynum == 1:
            return struct.pack("=6s", keyval)
        else:
           raise KeyError

f = TestFile()
f.open()
f.usekey(1)
f.rewind()

print 'Find', f.find(1, f.pack_key(1,'XXXXA3'))

for rec in f:
    print rec # prints out a record form DB file.


Results
Code:

Find True
Container:
    F01L001 = 'F01C'
    F01L002 = 'AC'
    F01L003 = 'F003AAAAC1'
    F01L004 = 'XXXXA3'
Container:
    F01L001 = 'F01D'
    F01L002 = 'AD'
    F01L003 = 'F003AAAAD1'
    F01L004 = 'XXXXA4'


So it seem to works.

What version of Python are you using ?

Regards,

JF
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forum.vmspython.org Forum Index -> Python for OpenVMS All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group