The function below allows you to submit multiple queries in one shot to a SQLITE database, and will return whatever you would get for the final query.
function sqlite_query_multi ($db, $query) {
// submit multiple queries (separated by ;) to $db
// and return the result from the last one
$multiSQL = "/('[^']*'|\"[^\"]*\"|[^;'\"])*;/";
preg_match_all ($multiSQL, "$query;", $aSQL);
for ($i=sizeof($aSQL=$aSQL[0]);$i--;)
if (!($trim=trim(substr($aSQL[$i],0,-1))))
unset ($aSQL[$i]);
else $aSQL[$i] = "$trim;";
foreach ($aSQL as $i => $sql)
$dbRes = sqlite_query ($db, $sql);
return (@$dbRes);
}
The section below illustrates the above function:
$db = sqlite_open(":memory:", 0666, $sqliteerror);
$query = <<<EOD
CREATE TABLE foo (bar INTEGER PRIMARY KEY, baz TEXT);
INSERT INTO foo VALUES (Null, 'Hi');
INSERT INTO foo VALUES (Null, 'Mom');
SELECT * FROM foo;
EOD;
$dbRes = sqlite_query_multi ($db, $query); // 4 statements
while (sqlite_has_more($dbRes))
var_dump(sqlite_fetch_array($dbRes, SQLITE_ASSOC));
Csaba Gabor