将此文本导入数据库

时间:2014-05-09 12:48:53

标签: php mysql sql import

我有一个主要的文本文件,我需要导入到数据库中,但老实说,我不知道如何。我尝试了一些爆炸,但我最终死了。

该文件的格式位于此帖的底部。 ID,名称,成员,可出租等都是列名。 ---之间的所有信息都是一行的一部分。任何人都可以把我推向正确的方向来帮我导入吗?

-------------------------------
ID: 0
Name: Dwarf remains
Members: true
Lendable: false
Stackable: false
Shop value: 0
Interface model: 2595
Model position x: -4
Model position y: 12
Model rotation x: 436
Model rotation y: 320
Model rotation z: 0
Model zoom: 1780
Interface options: [null, null, null, null, Destroy]
Ground options: [null, null, null, null, null]
Release: 27 May 2003
Quest: Dwarf Cannon
Tradeable: No
Destroy: These are the remains of the dwarf who was stationed in the Guard Tower.
Examine: The body of a Dwarf savaged by Goblins.
Weight: Unknown
-------------------------------
ID: 1
Name: Toolkit
Members: true
Lendable: false
Stackable: false
Shop value: 0
Interface model: 2679
Model position x: -3
Model position y: 2
Model rotation x: 2026
Model rotation y: 477
Model rotation z: 0
Model zoom: 1164
Interface options: [null, null, null, null, Destroy]
Ground options: [null, null, null, null, null]
Release: 27 May 2003
Quest: Dwarf Cannon
Tradeable: No
High alch: 0 coins
Low alch: 0 coins
Destroy: I got this from Captain Lawgof.
Examine: Good for repairing a broken cannon.
Weight: 0 kg
-------------------------------

1 个答案:

答案 0 :(得分:1)

首先,显然你需要调用该文件,然后你可以使用普通的'foreach并构建数组。考虑这个例子:

$contents = file('file1.txt');
$data = array();
$current_key = null;
foreach($contents as $key => $value) {
    $value = explode(':', $value);
    if(count($value) > 1) {
        if($value[0] == 'ID') {
            $current_key = $value[1];
            $data[$current_key]['ID'] = trim($value[1]);
            $columns[] = 'ID'; // prepare keys for insert 
        } else {
            $data[$current_key][$value[0]] = trim($value[1]);
            $columns[] = $value[0]; // prepare keys for insert
        }
    }   
}

print_r($data);
// gather your keys for insert
$columns = array_unique($columns);
foreach($columns as $key => &$value) {
    // make the columns look like columns
    $value = preg_replace('/[\s]+/', '_', strtolower(trim($value)));
}
$columns = implode(', ', $columns);

// format values
$values = '';
foreach($data as $key => $value) {
    $values .= "('" . implode("','", $value) . "'),";
}

// concatenate them (remove extra comma) 
$statement = "INSERT INTO table ($columns) VALUES " . substr($values, 0, -1);

print_r($statement);

示例输出:

Array
(
    [0] => Array
        (
            [ID] =>  0

            [Name] =>  Dwarf remains

            [Members] =>  true

            [Lendable] =>  false

            [Stackable] =>  false

            [Shop value] =>  0

            [Interface model] =>  2595

            [Model position x] =>  -4

            [Model position y] =>  12

            [Model rotation x] =>  436

            [Model rotation y] =>  320

            [Model rotation z] =>  0

            [Model zoom] =>  1780

            [Interface options] =>  [null, null, null, null, Destroy]

            [Ground options] =>  [null, null, null, null, null]

            [Release] =>  27 May 2003

            [Quest] =>  Dwarf Cannon

            [Tradeable] =>  No

            [Destroy] =>  These are the remains of the dwarf who was stationed in the Guard Tower.

            [Examine] =>  The body of a Dwarf savaged by Goblins.

            [Weight] =>  Unknown

        )

    [1] => Array
        (
            [ID] =>  1

            [Name] =>  Toolkit

            [Members] =>  true

            [Lendable] =>  false

            [Stackable] =>  false

            [Shop value] =>  0

            [Interface model] =>  2679

            [Model position x] =>  -3

            [Model position y] =>  2

            [Model rotation x] =>  2026

            [Model rotation y] =>  477

            [Model rotation z] =>  0

            [Model zoom] =>  1164

            [Interface options] =>  [null, null, null, null, Destroy]

            [Ground options] =>  [null, null, null, null, null]

            [Release] =>  27 May 2003

            [Quest] =>  Dwarf Cannon

            [Tradeable] =>  No

            [High alch] =>  0 coins

            [Low alch] =>  0 coins

            [Destroy] =>  I got this from Captain Lawgof.

            [Examine] =>  Good for repairing a broken cannon.

            [Weight] =>  0 kg

        )

)

示例声明:

INSERT INTO table (id, name, members, lendable, stackable, shop_value, interface_model, model_position_x, model_position_y, model_rotation_x, model_rotation_y, model_rotation_z, model_zoom, interface_options, ground_options, release, quest, tradeable, destroy, examine, weight, high_alch, low_alch) VALUES ('0','Dwarf remains','true','false','false','0','2595','-4','12','436','320','0','1780','[null, null, null, null, Destroy]','[null, null, null, null, null]','27 May 2003','Dwarf Cannon','No','These are the remains of the dwarf who was stationed in the Guard Tower.','The body of a Dwarf savaged by Goblins.','Unknown'),('1','Toolkit','true','false','false','0','2679','-3','2','2026','477','0','1164','[null, null, null, null, Destroy]','[null, null, null, null, null]','27 May 2003','Dwarf Cannon','No','0 coins','0 coins','I got this from Captain Lawgof.','Good for repairing a broken cannon.','0 kg')
  

重要说明:您可以将此作为参考,因为我不知道您的表结构是什么,所以列可能不同。