Fix memory corruption errors when copying strings for setting up sqlite database, fix errors in opening database under load by finalizing prepared statements even when no results returned