Click to See Complete Forum and Search --> : dbopen()


lazy_cod3R
01-26-2001, 09:20 PM
can the simple perl database hold more then one value for every key.
i want to try to get perl interface with a database using the dbi module and i could try it at home but the server where i wanna upload my script to doesn't have the dbi module installed, i dont really need a database which is that complex (i dont need foreign keys or anything like that) but complex enough so that for every key i can store more then one value.
(and i dont want to use textfiles because i just want to try something new )

i tried doing

dbopen(.....)
TNAME{"first"}=@name
but that would store the size of the array instead of the array itself.

i was going to try storing a reference to an array as the value but obviously once the program has finished executing the reference would have no use when i want to use the database again later on.

is it even possible to store more then one value for the simple built in perl database ?


another qstn i have is if i move my script to another server the data on database hosted on the old server will obviously be left there am i correct in assuming this ? , would ther be any way of importing it to the new server or will my only option be to write all the data to textfiles and maybe write another script to re-input them when i move to the new server .

thanx for any help

[This message has been edited by lazy_cod3R (edited 26 January 2001).]

TheLinuxDuck
01-26-2001, 09:41 PM
Originally posted by lazy_cod3R:
can the simple perl database hold more then one value for every key.

It's my understanding that a dmb is tied to a hash, which will only allow one vlaue per key, unless you decide to get fancy. http://www.linuxnewbie.org/ubb/smile.gif

I see two options.

1. Make the value of the key be a reference to an array. This way, you handle the value of the hash as an array, and you can put as many items in it you want.

2. Join the values together into one scalar. This you'll have to split when you want to use it, but it's an option.

Now, I don't have any experience with dmb files, so maybe these two things are not an option.. of course, if you use a reference to an array, you won't be able to save it into a file, you'll have to parse it out to a scalar before saving..

TNAME{"first"}=@name
but that would store the size of the array instead of the array itself.

If you put TNAME{"first"}=\@name;, you would be storing the reference. YOu could determine how many slices there are in the array by $#{TNAME{"first"}}, and you could print all items of the array by

for(@{TNAME{"first}}) {
print "$_\n";
}


Pretty qool stuff.. the fun part of using references like this is to make an array that is a reference to hashes. http://www.linuxnewbie.org/ubb/smile.gif

i was going to try storing a reference to an array as the value but obviously once the program has finished executing the reference would have no use when i want to use the database again later on.

You'd prolly have to do some kind of join function on the slices of the array, then assign that value to the hash value before saving.

another qstn i have is if i move my script to another server the data on database hosted on the old server will obviously be left there am i correct in assuming this ? , would ther be any way of importing it to the new server or will my only option be to write all the data to textfiles and maybe write another script to re-input them when i move to the new server .

Is there any reason why you couldn't just copy the database file from the old server to the new one?

------------------
TheLinuxDuck
I have a belly button.
:wq

lazy_cod3R
01-26-2001, 10:33 PM
Ok thanx alot u have solved the problem. i think i will use option 2 which u suggested, i can see how i would do that, option one which u suggested was also an option of mine before but it didnt work .

this is what i wrote before but nuthing was happening

@first=(1,2,3,4,5,6,7,8,9);
dbmopen(%FIRST,"first",0777);
$FIRST{"first"}=\@first;
foreach(@{$FIRST{"first"}}){
print "$_\n";
}


there was no out put and i didnt know what was wrong.

anyway what i might do is join the elems of the array and delimit them by ":" and then store that whole string as the value , later on i can split them again into an array.

thanx for the suggestion


Is there any reason why you couldn't just copy the database file from the old server to the new one?


hehe didn't see that a db file was created http://www.linuxnewbie.org/ubb/smile.gif
I have never tried using this function before.

[This message has been edited by lazy_cod3R (edited 26 January 2001).]

jemfinch
01-28-2001, 01:05 AM
Look into the MLDBM module if you don't want to have to do explicit joins/splits when you access non-scalar data in a tied hash. That won't, however, solve all your problems; if you read the MLDBM documentation page, you'll find there's something (I can't remember exactly at the moment, and I'm not at my computer) that you can't do that's a relatively common thing. I think it was that you have you retrieve the value and assign a new value in two separate statements; something like "$tiedmldbm{'key'} = ($tiedmldbm{'key'} =~ s/foo/bar/g);" won't work. I'm probably wrong, though.

Using python and its shelve module would solve all your problems, btw.

Jeremy