Garrett, Philip (MAN-Corporate) wrote: > > George Bills wrote: > > I've also seen MySQL syntax like "ON DUPLICATE KEY > > UPDATE ..." which looks good, but I'd like to avoid an extra > > dependency on a database that has to be set up and maintained (hence > > SQLite). > > > > My preference for a solution would be something that's simple and > > maintainable primarily, followed by fast (for reads first, writes > > second) - but I don't have the database experience to know what the > > best solution is. Is there an easy way? > > Yes, but it looks like you've already done it: try fetch, update if > successful, insert otherwise.
This will work as long as you don't have to worry about concurrent processes inserting rows into the table. If you do have that, then you've created a race condition. Two processes could try fetch at roughly the same time, fail, and insert the same row, causing one of the processes to get a duplicate key error. Instead you could insert while trapping errors, and update if a duplicate key error occurs. However, that probably requires checking for a database-specific error code or message, at which point you might as well just take advantage of MySQL's ON DUPLICATE KEY UPDATE. Ronald
