note 42027 deleted from function.sqlite-query by cmb
| From: | cmb@php.net | Date: | Mon, 02 Nov 2020 15:02:59 +0000 |
| Subject: | note 42027 deleted from function.sqlite-query by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215755@lists.php.net to get a copy of this message | ||
Note Submitter: csaba at alum dot mit dot edu
----
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