gpt4 book ai didi

php - 获取文件的详细信息然后插入到数据库

转载 作者:搜寻专家 更新时间:2023-10-30 23:17:54 25 4
gpt4 key购买 nike

我有一个 .txt 文件,其中包含所有国家/地区的详细信息,格式如下:

Country,City,AccentCity,Region,Population,Latitude,Longitude
ad,aixas,Aixàs,06,,42.4833333,1.4666667
ad,aixirivali,Aixirivali,06,,42.4666667,1.5
ad,aixirivall,Aixirivall,06,,42.4666667,1.5
ad,aixirvall,Aixirvall,06,,42.4666667,1.5
ad,aixovall,Aixovall,06,,42.4666667,1.4833333
ad,andorra,Andorra,07,,42.5,1.5166667
ad,andorra la vella,Andorra la Vella,07,20430,42.5,1.5166667
ad,andorra-vieille,Andorra-Vieille,07,,42.5,1.5166667
ad,andorre,Andorre,07,,42.5,1.5166667
ad,andorre-la-vieille,Andorre-la-Vieille,07,,42.5,1.5166667
ad,andorre-vieille,Andorre-Vieille,07,,42.5,1.5166667
ad,ansalonga,Ansalonga,04,,42.5666667,1.5166667

我必须将这些数据插入到 3 个表中,例如城市、州和国家/地区,而不要重复。

有什么方法可以从 .txt 文件中读取数据并将其插入数据库?

如何获取州和城市数据库?

最佳答案

以 CSV 格式读取文件,然后插入:

if (($handle = fopen("cities.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
// Craft your SQL insert statement such as:
$sql = "INSERT INTO cities (country, city, accent_city, etc.) VALUES ('{$data[0]}','{$data[1]}','{$data[2]}', etc.)";
// Use the appropriate backend functions depending on your DB, mysql, postgres, etc.
}
}

关于php - 获取文件的详细信息然后插入到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10650973/

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