【问题标题】:Import CSV File into a SQLite Database via PHP通过 PHP 将 CSV 文件导入 SQLite 数据库
【发布时间】:2014-08-18 13:07:42
【问题描述】:

我想通过我可以手动运行以更新数据的 PHP 脚本将 CSV 文件中的表导入 SQLite DB。

这是我想要实现的目标列表:

  1. 将旧表(称为“produkte”)重命名为 product-currentdate(或删除表)
  2. 然后从 CSV 文件导入文件(; 分隔和 ISO 8859-1 字符集/CSV 文件的第一行包含表头)
  3. 在“产品”表中保存日期

我发现一个脚本由于某种原因不起作用:

<?php
 $dir = 'sqlite:test.sqlite';
 $dbh  = new PDO($dir) or die("cannot open the database");


 $query = <<<eof
  LOAD DATA LOCAL INFILE 'produkte.csv'
  INTO TABLE produkte
  FIELDS TERMINATED BY ';'
  OPTIONALLY ENCLOSED BY '"' 
  LINES TERMINATED BY '\n'
  IGNORE 1 LINES 
  (id, Hauptmenue, Produktgruppe, Beschreibung, Text, Bild, Shop, Info)

 eof;

 $dbh->query($query);

?>

我希望有人知道如何解决我的问题...

最好的问候戴夫

【问题讨论】:

  • 因为这两个功能都不适合你,我建议你检查你的文件是否有错误

标签: php sql sqlite csv


【解决方案1】:

Federico Cingolani 已在Github 发布了满足您需求的 php 脚本

 <?php
function import_csv_to_sqlite(&$pdo, $csv_path, $options = array())
{
    extract($options);

    if (($csv_handle = fopen($csv_path, "r")) === FALSE)
        throw new Exception('Cannot open CSV file');

    if(!$delimiter)
        $delimiter = ',';

    if(!$table)
        $table = preg_replace("/[^A-Z0-9]/i", '', basename($csv_path));

    if(!$fields){
        $fields = array_map(function ($field){
            return strtolower(preg_replace("/[^A-Z0-9]/i", '', $field));
        }, fgetcsv($csv_handle, 0, $delimiter));
    }

    $create_fields_str = join(', ', array_map(function ($field){
        return "$field TEXT NULL";
    }, $fields));

    $pdo->beginTransaction();

    $create_table_sql = "CREATE TABLE IF NOT EXISTS $table ($create_fields_str)";
    $pdo->exec($create_table_sql);

    $insert_fields_str = join(', ', $fields);
    $insert_values_str = join(', ', array_fill(0, count($fields),  '?'));
    $insert_sql = "INSERT INTO $table ($insert_fields_str) VALUES ($insert_values_str)";
    $insert_sth = $pdo->prepare($insert_sql);

    $inserted_rows = 0;
    while (($data = fgetcsv($csv_handle, 0, $delimiter)) !== FALSE) {
        $insert_sth->execute($data);
        $inserted_rows++;
    }

    $pdo->commit();

    fclose($csv_handle);

    return array(
            'table' => $table,
            'fields' => $fields,
            'insert' => $insert_sth,
            'inserted_rows' => $inserted_rows
        );

}

【讨论】:

  • 非常感谢 :) options 数组长什么样子?
  • 谁能给我一个如何执行函数的例子(我需要参数..)
  • 所以如果我有:$dir = 'sqlite:test.sqlite'; $dbh = new PDO($dir) or die("cannot open the database"); $csv = 'produkte.csv'; 然后我像这样调用函数?:import_csv_to_sqlite(&amp;$dbh, $csv);
  • 我收到一个错误:Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /path-to/getcsvdata.php on line 20,第 20 行显示:$fields = array_map(function ($field){
  • no changes.... 我使用了以下代码:我从 Github 复制了版本,然后像这样调用函数:import_csv_to_sqlite(&amp;new PDO('sqlite:test.sqlite'),'produkte.csv', $options=array());
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-21
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多