[ZODB-Dev] Problems when reading from ZODB
Luis Machado
luis.machado at mandioca.tzo.com
Sat May 17 19:34:05 EDT 2003
I'm just starting to play with ZODB and I can not find what I'm doing
wrong in the following programs.
Customer.py
-------------------------------
import ZODB
from Persistence import Persistent
class Customer(Persistent):
def __init__(self):
self.company = ''
self.name = ''
self.address1 = ''
self.address2 = ''
self.city = ''
self.state = ''
self.zip = ''
self.country = ''
self.phone = ''
self.fax = ''
self.web = ''
self.email = ''
----------------------------------
The following program writes 100 customers into ZODB
from Customer import Customer
from ZODB import FileStorage, DB
storage =
FileStorage.FileStorage('/home/lmachado/dev/zodb/filestorage01.fs')
db = DB(storage)
conn = db.open()
dbroot = conn.root()
if dbroot.has_key('customerDB'):
del dbroot['customerDB']
if not dbroot.has_key('customerDB'):
from BTrees import OOBTree
customerDB = OOBTree.OOBTree()
dbroot['customerDB'] = customerDB
get_transaction().commit()
c = Customer()
for i in range (1,100):
c.company = 'Any Company' + '-' + str(i)
c.name = 'Jim Brown' + '-' + str(i)
c.address1 = '15230 Lexington Blv' + '-' + str(i)
c.address2 = ''
c.city = 'Sugar Land' + '-' + str(i)
c.state = 'TX' + '-' + str(i)
c.zip = '77478' + '-' + str(i)
c.country = 'USA' + '-' + str(i)
c.phone = str(i)
c.web = 'www.anycompany.com' + '-' + str(i)
c.email = 'jim.brown at anycompany.com' + '-' + str(i)
c.fax = '15345232' + '-' + str(i)
c_p_changed = 1
customerDB.insert(c.company,c)
customerDB._p_changed = 1
get_transaction().commit()
conn = db.close()
-----------------------------------------
So far, this works fine, but when I tried to read the customers from the
ZODB database, no matter what is the key, for name, address1, city, etc,
I always get the last value stored in the database.
This is the program that I'm using to read from ZODB
from Customer import Customer
from ZODB import FileStorage, DB
from BTrees import OOBTree
storage =
FileStorage.FileStorage('/home/lmachado/dev/zodb/filestorage01.fs')
db = DB(storage)
conn = db.open()
dbroot = conn.root()
customerDB = dbroot['customerDB']
for k in customerDB.keys():
c = customerDB.__getitem__(k)
print k + ' - ' + c.name
conn = db.close()
For example, for k='Any Company-75', c.name is 'Jim Brown-99'. For
k='Any Company-23', c.name='Jim Brown-99' and so on.
Any help will be appreciated.
Luis
More information about the ZODB-Dev
mailing list