|
16 | 16 | define('PASS', ''); |
17 | 17 | define('DB', 'test'); |
18 | 18 |
|
19 | | -////////////////////////////////////////////////////////////////// |
20 | | -$db = new MySQL_wrapper(HOST, USER, PASS, DB); |
21 | | -$db->connect(); |
22 | | - |
23 | | -$db->dropTable('csv_to_table_test'); |
24 | | -$db->createTableFromCSV('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n'); |
25 | | - |
26 | | -$db->dropTable('csv_to_table_test_no_column_names'); |
27 | | -$db->createTableFromCSV('test_files/countrylist1.csv', 'csv_to_table_test_no_column_names', ',', '"', '\\', 0, array(), 'generate', '\r\n'); |
28 | | - |
29 | | -/** Create table from CSV file and imports CSV data to Table with possibility to update rows while import. |
30 | | - * @param string $file - CSV File path |
31 | | - * @param string $table - Table name |
32 | | - * @param string $delimiter - COLUMNS TERMINATED BY (Default: ',') |
33 | | - * @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"') |
34 | | - * @param string $escape - ESCAPED BY (Default: '\') |
35 | | - * @param integer $ignore - Number of ignored rows (Default: 1) |
36 | | - * @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1') |
37 | | - * @param string $getColumnsFrom - Get Columns Names from (file or generate) - this is important if there is update while inserting (Default: file) |
38 | | - * @param string $newLine - New line delimiter (Default: \n) |
39 | | - * @param resource $link - Link identifier |
40 | | - * @return number of inserted rows or false |
41 | | - */ |
42 | | -// function createTableFromCSV($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\r\n', $link = 0) |
43 | | - |
44 | | -$db->close(); |
45 | | -exit; |
46 | | -/////////////////////////////////////////////////////////////////// |
47 | | - |
48 | 19 | // create test table |
49 | 20 | $db = new MySQL_wrapper(HOST, USER, PASS, DB); |
50 | 21 | $db->connect(); |
|
588 | 559 | /////////////////////////////////////////////////////////////////////////////////////////// |
589 | 560 |
|
590 | 561 | // Example 21 |
| 562 | +// Import CSV to Table |
| 563 | +/////////////////////////////////////////////////////////////////////////////////////////// |
| 564 | +$db = new MySQL_wrapper(HOST, USER, PASS, DB); |
| 565 | +// Connect |
| 566 | +$db->connect(); |
| 567 | +// Import and update all data |
| 568 | +$db->importUpdateCSV2Table('test_files/countrylist.csv', 'csv_to_table_test', ',', '"', '\\', 1, array(), 'file', '\r\n'); |
| 569 | +// More options |
| 570 | +/** Imports (ON DUPLICATE KEY UPDATE) CSV data in Table with possibility to update rows while import. |
| 571 | + * @param string $file - CSV File path |
| 572 | + * @param string $table - Table name |
| 573 | + * @param string $delimiter - COLUMNS TERMINATED BY (Default: ',') |
| 574 | + * @param string $enclosure - OPTIONALLY ENCLOSED BY (Default: '"') |
| 575 | + * @param string $escape - ESCAPED BY (Defaul: '\') |
| 576 | + * @param integer $ignore - Number of ignored rows (Default: 1) |
| 577 | + * @param array $update - If row fields needed to be updated eg date format or increment (SQL format only @FIELD is variable with content of that field in CSV row) $update = array('SOME_DATE' => 'STR_TO_DATE(@SOME_DATE, "%d/%m/%Y")', 'SOME_INCREMENT' => '@SOME_INCREMENT + 1') |
| 578 | + * @param string $getColumnsFrom - Get Columns Names from (file or table) - this is important if there is update while inserting (Default: file) |
| 579 | + * @param string $newLine - New line detelimiter (Default: \n) |
| 580 | + * @param resource $link - link identifier |
| 581 | + * @return number of inserted rows or false |
| 582 | + */ |
| 583 | +// $db->importUpdateCSV2Table($file, $table, $delimiter = ',', $enclosure = '"', $escape = '\\', $ignore = 1, $update = array(), $getColumnsFrom = 'file', $newLine = '\n', $link = 0) |
| 584 | +// Close connection |
| 585 | +$db->close(); |
| 586 | +/////////////////////////////////////////////////////////////////////////////////////////// |
| 587 | + |
| 588 | +// Example 22 |
591 | 589 | // Transactions |
592 | 590 | /////////////////////////////////////////////////////////////////////////////////////////// |
593 | 591 | $db = new MySQL_wrapper(HOST, USER, PASS, DB); |
|
609 | 607 | // Close connection |
610 | 608 | $db->close(); |
611 | 609 |
|
612 | | -// Example 22 |
| 610 | +// Example 23 |
613 | 611 | // String Replace Table Columns |
614 | 612 | /////////////////////////////////////////////////////////////////////////////////////////// |
615 | 613 | $db = new MySQL_wrapper(HOST, USER, PASS, DB); |
|
0 commit comments