My code looks like this:
// Connect to SQLite DB
DB('/path/to/sqlite.db');
DB('BEGIN TRANSACTION;');
// These loops are just examples.
for ($i = 1; $i <= 10000; $i++)
{
for ($j = 1; $j <= 100; $j++)
{
DB('INSERT INTO "test" ("id", "name") VALUES (?, ?);', $i, 'Testing ' . $j);
}
}
DB('END TRANSACTION;');
And here is the DB() function:
function DB($query)
{
static $db = null;
if (is_file($query) === true)
{
$db = new PDO('sqlite:' . $query, null, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
}
else if (is_a($db, 'PDO') === true)
{
$result = $db->prepare($query);
if (is_a($result, 'PDOStatement') === true)
{
if ($result->execute(array_slice(func_get_args(), 1)) === true)
{
if (stripos($query, 'INSERT') === 0)
{
return $db->lastInsertId();
}
if (stripos($query, 'SELECT') === 0)
{
return $result->fetchAll(PDO::FETCH_ASSOC);
}
return $result->rowCount();
}
}
return false;
}
return true;
}
The problem is that the DB() call inside the inner loop is taking to long to complete, I was thinking that doing something like this could speed up considerably the execution:
DB('BEGIN TRANSACTION;');
for ($i = 1; $i <= 10000; $i++)
{
$queries = array();
for ($j = 1; $j <= 100; $j++)
{
$queries[] = 'INSERT INTO "test" ("id", "name") VALUES (?, ?);' /*, $i, 'Testing ' . $j;*/
}
DB(implode("\n", $queries));
}
DB('END TRANSACTION;');
The problem is I don't know how I could prepare (replace and escape) all the question marks with the respective variables, is there any way I can accomplish this?