db2_txt转mysql

$directory_arr = scandir($path);
unset($directory_arr[0]);//删除 .
unset($directory_arr[1]);//删除 ..

$directory_arr = array_values($directory_arr);//重置index

for ($j=0;$j<count($directory_arr);$j++){

    $f = fopen($path.'/'.$directory_arr[$j],'r');
    $rows = [];
    while (!feof($f)){
        $rows[] = fgets($f);
    }
    fclose($f);

    preg_match('/\."(.+)"/', $rows[1], $matches);
    $table_name = $matches[1];

    $cols = [];

    for ($i = 2 ; count($rows)>$i ; $i++){
        $row = trim($rows[$i]," \t\n\r\0\x0B");
        if (empty($row)){
            continue;
        }
        preg_match('/"(.+)"/', $row, $matches);
        $col = $matches[1];
        $row = preg_replace('/"/', '`', $row);
        if ($row[strlen($row)-1] !== ','){
            $row = substr($row, 0, -1);
            $cols[$col] = $row;
            break;
        }
        $row = substr($row, 0, -1);
        $cols[$col] = $row;
    }
    for ($i = 0 ; count($rows)>$i ; $i++){
        $row = trim($rows[$i]," \t\n\r\0\x0B");
        if (empty($row)){
            continue;
        }
        if (substr($row,0,17) === 'COMMENT ON COLUMN'){
            preg_match('/\.".+"\."(.+)" IS/', $row, $matches);
            $col = $matches[1];
            preg_match('/IS \'(.+)\'/', $row, $matches);
            $comment = $matches[1];
            $cols[$col] = $cols[$col].' COMMENT \''.$comment.'\'';
        }
    }
    $key = '';
    for ($i = 0;$i<count($rows);$i++){
        if (substr($rows[$i],0,11) === 'ALTER TABLE'){
            preg_match('/\"(.+)\"/', $rows[$i+2], $matches);
            $key = $matches[1];
        }
    }

    $cols['key'] = "PRIMARY KEY (`".$key."`) USING BTREE";
    $cols['index'] = "INDEX `REPORTID`(`REPORTID`) USING BTREE";

    $sql = 'create table `'.$table_name.'` ('.implode(',',array_values($cols)).' ) ENGINE = MyISAM';


    if ($link->query($sql) === TRUE) {
        printf("Table myCity successfully created.\n");
    }else{
        printf($directory_arr[$j].' False'."\n");
    }

}

 

posted @ 2022-03-17 13:08  树之下  阅读(68)  评论(0)    收藏  举报