Insert Sqlite using an array - PHP/Laravel

Asked

Viewed 188 times

1

I have the following array:

array:30 [▼
  0 => "12933"
  1 => "12931"
  2 => "12930"
  3 => "12929"
  4 => "12928"
  5 => "12927"
  6 => "12926"
  7 => "12925"
  8 => "12924"
  9 => "12923"
  10 => "12922"
  11 => "12921"
  12 => "12920"
  13 => "12919"
  14 => "12918"
  15 => "12917"
  16 => "12916"
  17 => "12915"
  18 => "12914"
  19 => "12913"
  20 => "12912"
  21 => "12911"
  22 => "12910"
  23 => "12909"
  24 => "12908"
  25 => "12907"
  26 => "12906"
  27 => "12905"
  28 => "12904"
  29 => "12903"
]

My application is using a production base SQLServer and I have another base SQLITE that I have only 2 query tables and on that basis, a table called Notas with only one column of name Numero.

I’m trying to give a insert in this table, where each array would be a record, however, as I never did this, I’m not getting it, I’m trying the following code:

$insertNumeroNotas = DB::connection('sqlite')
     ->insert('insert into Notas (Numero) values (?)', $quantidadeNotas);

in which $quantidadeNotas is the array.

I need to first iterate through this array and then give the insert, or the insert needs to be given in each iteration of that array?

  • fjurr gave the right answer?

  • gave yes Virgilio, thanks anyway

  • if you can accept as a reply @fjurr

  • 1

    ready, I had forgotten it... thanks!

1 answer

1


Make a foreach and surround the code of your question by changing the parameter so that each interaction records the item of each position of the array:

foreach($quantidadeNotas as $n)
{
    DB::connection('sqlite')
        ->insert('insert into Notas (Numero) values (?)', [$n]);
}

that way would one of the ways the other would be with:

DB::connection('sqlite')
      ->table('Notas')
      ->insert(array_map(function($i)
            { return ['Numero' => $i]; }, 
       $quantidadeNotas));

is another way where the algorithm itself solves the array.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.