jeudi 11 octobre 2018

Insert a bunch of data after generating it

For seeding my database I need to generate a query string. The first task was to find a way to quickly insert a bunch of data in a MySQL database. Eventually I found this. But I still need to generate the query string itself.
The table consists of three rows. Two columns filled with integers and one filled with a character.
One row represents a mathematical task. So I want them to look like

1 + 1
1 + 2
...
1 + 1000
...
2 + 1
...
1000 + 1000

My approach for that was
$string = ""
for($i = 1; $i <=1000; $i++)
{
   for($j = 1; $j <=1000; $j++){
   $string = $string . ", ({$i}, +, {$j}");
   }
}

But this is obviously slow.
I thought I would tackle this by generating two arrays and generate the cartesian product but it didn't work out so far.

$array1 = array();
$array2 = array();
for($i = 1; $i <=1000; $i++)
{
   array_push($array1, ", ({$i}, +, ");
   array_push($array2, "{$i})");
}
//generate some kind of cartesian product here. 

Using something like

$array1 = array();
$array2 = array();
for($i = 1; $i <=1000; $i++)
{
   array_push($array1, ", ({$i}, +, ");
   array_push($array2, "{$i})");
}
//generate some kind of cartesian product here.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire