1

この形式のすべての国の詳細を含む.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 ファイルからデータを読み取ってデータベースに挿入する方法はありますか?

州と都市のデータベースを取得するにはどうすればよいですか?

4

2 に答える 2

2

ファイルを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.
    }
}
于 2012-05-18T10:41:45.200 に答える
0

データベースがmysqlの場合、一括挿入するユーティリティが存在し、ドキュメント

そうでない場合は、おそらくデータベースにもあると思いますが、PHP を使用してこれを行いたい場合は、@davidethell の例が適しています。

于 2012-05-18T10:57:55.457 に答える