Domanda

I have a table structure like this:

id
assigned_by
assigned_to_user_id
vendor_id
receiver_no
purchase_order_no
destination
po_status
invoice_no
invoice_amount
delivery_date
datetime_done
latest_jda_sync_date
latest_mobile_sync_date
created_at
updated_at
deleted_at

And the content of my csv file is like this:

vendor_id receiver_no purchase_order_no destination po_status
30105   20110   10151   9005    3
50015   20114   10155   9005    3

And right now I'm able to load the csv file and insert them in my database. I have this script below.

$columns = "(@dummy, @dummy, @dummy, vendor_id, receiver_no, purchase_order_no, destination, po_status, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy, @dummy)";
$affectedRows = $pdo->exec("
    LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
      FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
      LINES TERMINATED BY ".$pdo->quote($lineseparator)." ". $columns);

The only problem I'm having is inserting the data to the correct column. Can anyone help me.

È stato utile?

Soluzione

As documented under LOAD DATA INFILE Syntax:

When processing an input line, LOAD DATA splits it into fields and uses the values according to the column/variable list and the SET clause, if they are present.

In other words, the column list should describe the database columns (or user variables) to which each the input fields should be assigned (rather than describing in which input field each database column can be found). This is, of course, obvious when one realises that the input file need not contain field names and thus it would be impossible to adopt the latter approach in all circumstances.

Therefore, you want:

$columns = '(vendor_id, receiver_no, purchase_order_no, destination, po_status)';

You will also need to add IGNORE 1 LINES to the command in order to skip the first line (with field names):

$affectedRows = $pdo->exec("
    LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
      FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
      LINES TERMINATED BY ".$pdo->quote($lineseparator)."
      IGNORE 1 LINES ". $columns);

Altri suggerimenti

You need to specify which columns you want to use (it seems you have them mucked about in order of how your statement executes):

LOAD DATA INFILE 'file.csv'
  INTO TABLE t1
  (column1, @dummy, column2, @dummy, column3, ...)
FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';

See the manual for more details.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top