in reply to Re: writing a file
in thread writing a file

And, of course, add some error-checking to the database query itself...