How to make create_or_update in sqlobject?

I am using SQLobject and still have not been able to find an elegant solution for updating a row in db or creating a new one if it does not exist.

I am currently using the following somewhat convoluted code:

args = dict(artnr=artnr, name=name, hersteller='?', hersteller_name='?')
if cs.datamart.ArtNrNameHersteller.selectBy(artnr=artnr).count():
    row = cs.datamart.ArtNrNameHersteller.selectBy(artnr=artnr)[0]
    row.set(**args)
else:
    cs.datamart.ArtNrNameHersteller(**args)

Obviously, this is far from insane, strong, elegant or fast. What is the correct way (TM) for this?

+3
source share
2 answers

- , , . , SQL, (*), , .

(*: MySQL ON DUPLICATE KEY UPDATE REPLACE, UNIQUE , , Oracle OGLE-as-sin MERGE , SQLObject .)

+1

SQLObject API . SQLBuilder Update, Replace Update.

+1

Source: https://habr.com/ru/post/1715801/


All Articles