DataBase Bulk Update

DataBase Bulk Update

by Bryan Holladay -
Number of replies: 1

Does Moodle's DB api have a way to bulk update existing and non-existing items with different values? For example, I receive a bunch of values from an REST API and want to update the Moodle DB table with these values.

id=1 val=4

id=2 val=20

id=3 val=24

...


I'm doing it through a loop right now with "$DB->update_record()" or "$DB->insert_record()". I saw that there is a "bulk" value you can pass, but it looks like it's not even implemented in the mysql DB implementation. I was thinking of doing a DB->exec() with something like:

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);

But I don't think that works for Ansi or even Oracle and I don't want to restrict for specific DBMS's

Are there any examples or APIs that I should use instead?

Thanks
Bryan


Average of ratings: Useful (1)
In reply to Bryan Holladay

Re: DataBase Bulk Update

by Justin Hunt -
Picture of Particularly helpful Moodlers Picture of Plugin developers

You might first construct a query to check for records that exist, then do a bulk insert as described on this page for those that don't exist..

https://docs.moodle.org/dev/Data_manipulation_API

For the others you might have to do it one by one, or create a temporary table and do it by referencing that. If this is a plugin you could simplify it by maintaining a table just for this people, and just clear it before you start each job.,