fix for #10012

the sql statement in image.c:782 was just terribly wrong. while trying to handle
grouped images it would overwrite existing xmp files in adverse conditions.

I am still not 100% sure about the use of sqlite3_finalize(stmt) in that whole function but did not change
anything there.