Synced sqlite3utils with private and fixed a small recursive bug in SIPEngine that was already fixed in private

git-svn-id: http://wush.net/svn/range/software/public/openbts/trunk@4912 19bc5d8c-e614-43d4-8b26-e1612bc8e597
diff --git a/CommonLibs/sqlite3util.cpp b/CommonLibs/sqlite3util.cpp
index b898445..036ef2c 100644
--- a/CommonLibs/sqlite3util.cpp
+++ b/CommonLibs/sqlite3util.cpp
@@ -1,6 +1,6 @@
 /*
-* Written by David A. Burgess, Kestrel Signal Processing, Inc., 2010
-* The author disclaims copyright to this source code.
+* Copyright 2010 Kestrel Signal Processing, Inc.
+* All rights reserved.
 */
 
 
@@ -17,12 +17,18 @@
 
 int sqlite3_prepare_statement(sqlite3* DB, sqlite3_stmt **stmt, const char* query)
 {
-	int prc = sqlite3_prepare_v2(DB,query,strlen(query),stmt,NULL);
-	if (prc) {
-		fprintf(stderr,"sqlite3_prepare_v2 failed for \"%s\": %s\n",query,sqlite3_errmsg(DB));
-		sqlite3_finalize(*stmt);
-	}
-	return prc;
+        int src = SQLITE_BUSY;
+        while (src==SQLITE_BUSY) {
+                src = sqlite3_prepare_v2(DB,query,strlen(query),stmt,NULL);
+                if (src==SQLITE_BUSY) {
+                        usleep(100000);
+                }
+        }
+        if (src) {
+                fprintf(stderr,"sqlite3_prepare_v2 failed for \"%s\": %s\n",query,sqlite3_errmsg(DB));
+                sqlite3_finalize(*stmt);
+        }
+        return src;
 }
 
 int sqlite3_run_query(sqlite3* DB, sqlite3_stmt *stmt)
@@ -140,6 +146,7 @@
 	if (sqlite3_prepare_statement(DB,&stmt,query)) return false;
 	// Run the query.
 	int src = sqlite3_run_query(DB,stmt);
+	sqlite3_finalize(stmt);
 	return src==SQLITE_DONE;
 }