php - 如何生成 MySQL INSERT 语句以使用数组中的多行填充表
Posted
技术标签:
【中文标题】php - 如何生成 MySQL INSERT 语句以使用数组中的多行填充表【英文标题】:php - How to generate MySQL INSERT statements to populate a table with multiple rows from array 【发布时间】:2021-07-20 23:09:04 【问题描述】:我已读入一个 CSV 文件,其中包含超过 4000 行的配方数据。索引 7 是诸如“早餐”、“素食主义者”等类别。我将在 mysql 中在食谱及其类别之间创建多对多表,但首先我为类别表生成 INSERT
语句。我需要删除重复值(其中有很多),并使用array_unique()
成功完成了这项工作。
我正在生成插入语句,而不是直接将数据放入我的数据库,因为我仍然不确定自己在做什么。
这是我的代码:
<?php
include("connection.php");
$file = "recipes.csv";
$categories = array();
if (file_exists($file))
$filepath = fopen($file, "r");
while( ($line = fgetcsv($filepath)) !== FALSE )
$categories[] = $line[7];
$categories = array_unique($categories);
$catstr = implode(" , ",$categories);
echo "INSERT INTO category (categoryname) VALUES ('".$catstr."')";
echo "<br>";
我不确定是否使用 implode 函数,也许我用错了。
这是输出:
INSERT INTO category (categoryname) VALUES ('European , Dessert')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces , Low Protein')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces , Low Protein , < 15 Mins')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces , Low Protein , < 15 Mins')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces , Low Protein , < 15 Mins')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces , Low Protein , < 15 Mins , Cheesecake')
INSERT INTO category (categoryname) VALUES ('European , Dessert , < 30 Mins , Steak , Chicken Breast , Lunch/Snacks , Breakfast , Candy , Ice Cream , Chicken , Gelatin , Meat , Sauces , Low Protein , < 15 Mins , Cheesecake')
....等等。
我的代码显然会为每个原始索引值输出一些内容,并在每次迭代时输出所有先前的类别值。
我需要它做什么:
INSERT INTO category (categoryname) VALUES ('European')
INSERT INTO category (categoryname) VALUES ('Dessert')
INSERT INTO category (categoryname) VALUES ('< 30 Mins')
INSERT INTO category (categoryname) VALUES ('Steak')
INSERT INTO category (categoryname) VALUES ('Chicken Breast')
INSERT INTO category (categoryname) VALUES ('Lunch/Snacks')
INSERT INTO category (categoryname) VALUES ('Breakfast')
etc
任何帮助将不胜感激。
【问题讨论】:
为什么不分享文件的摘录(例如前 5 行、中间 5 行和最后 5 行) 【参考方案1】:你正在寻找的是这样的:
<?php
$db = new \PDO($dsn, $username, $password, $options);
$file = "recipes.csv";
if (file_exists($file))
$stmt = $db->prepare("INSERT INTO category SET categoryname = ?");
$filepath = fopen($file, "r");
while(($line = fgetcsv($filepath)) !== false)
$categories[] = $line[7];
$categories = array_unique($categories);
foreach ($categories as $cat)
$stmt->execute([$cat]);
首先循环遍历 CSV,以便获取唯一值,然后使用准备好的语句依次插入每个值。这假设您使用的是 PDO,显然需要添加一些错误检查代码。
【讨论】:
您好,感谢您的回复。我正在使用 MySQLi 你不应该这样。如果你想继续,这里有很多关于如何用它做准备好的陈述的材料。【参考方案2】:添加每一行的最后一个元素并不是很复杂。因为每次类别都是唯一的,并且最后一个元素被定义为每一行的新类别
<?php
// include("connection.php");
$file = "recipes.csv";
$categories = array();
if (file_exists($file))
$filepath = fopen($file, "r");
while( ($line = fgetcsv($filepath)) !== FALSE )
$cat =end($line);
echo "INSERT INTO category (categoryname) VALUES ('". $cat."')";
echo "<br>";
【讨论】:
以上是关于php - 如何生成 MySQL INSERT 语句以使用数组中的多行填充表的主要内容,如果未能解决你的问题,请参考以下文章
如何在 PHP 中从 AUTO_INCREMENT 列中为每行检索生成的 PRIMARY KEY,并在 MySQL 中插入多行?