gpt4 book ai didi

php - 从表中删除列

转载 作者:行者123 更新时间:2023-11-29 11:33:37 24 4
gpt4 key购买 nike

我有一个 .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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com