openlca-python-tutorial
openlca-python-tutorial copied to clipboard
Error when attempting to delete an entity
Hi, enjoying learning this api, thank you!
I haven't been able to delete entities using the dao. i am using openLCA 1.6.3, and the ide and jython version specified here. This example is for unit groups, but I have also had the error with processes.
Thanks in advance!
My code is something like:
path = "my path"
db = DerbyDatabase(File(path))
dao = UnitGroupDao(db)
myunit = dao.getForName("My Special Units")
dao.delete(myunit)
And the error I get is this:
dao.delete(myunit)
at org.openlca.core.database.DatabaseException.logAndThrow(DatabaseException.java:16)
at org.openlca.core.database.BaseDao.delete(BaseDao.java:106)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
org.openlca.core.database.DatabaseException: org.openlca.core.database.DatabaseException: Error while deleting UnitGroup
Correct. This is probably not documented but the getForName
method returns a list because in openLCA it is allowed to have the same name for a thing like unit group, flow, process etc. multiple times:
import org.openlca.core.database.UnitGroupDao as UnitGroupDao
dao = UnitGroupDao(db)
unit_groups = dao.getForName('Cheese weight units')
log.info('found {} unit group(s)', len(unit_groups))
for ug in unit_groups:
dao.delete(ug)
Thanks! :)