我有一个.csv文件数据正在导入到mysql表中。每次运行代码时,我想做的是我想删除所有列和数据,并用更新的.csv内容替换它。

   $affectedRows = $pdo->exec("
     //HERE I do need additional code to drop column data from $databasetable similar to following
  /*------------------------------------------*/
    DROP TABLE IF EXISTS '$databasetable'

    CREATE TABLE IF NOT EXISTS `$databasetable` (
          Column names1
          Column name 2
          .
     ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
  /*------------------------------------------*/


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

最佳答案

我认为您想要做的是截断有问题的表。那就是删除表中的所有数据,而不是删除表本身,对吗?
因此,这里有1条提示:

<?php
    //FIRST TRUNCATE (REMOVE OLD DATA IN THE TABLE $databasetable
    $stmt      = $db->prepare("TRUNCATE TABLE {$databasetable}");
    $tblEmpty  = $stmt->execute();

    //NOW, ONCE THE TABLE'S BEEN TRUNCATED; YOU MAY CONTINUE WITH THE NEW "INSERTS"...
    if($tblEmpty){
        // INSERT THE DATA FROM THE CSV...
    }

    //...
    //...
    // MORE CODE HERE...

关于php - 从表中删除列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36988484/

10-10 11:52