注:是装XML文件的内容,不是将XML作为二进制存储。
例如:有个表table1,存在3个字段a,b,c。现在将这3个字段的数值存到XML文件中,装载时直接入库。请问,怎么让Mysql读取这个XML格式文件。load data infile 的那些个命令能用吗

解决方案 »

  1.   

    不支持直接导入!请参考《O'Reilly: Mysql Cookbook》中的内容:
    10.42 Exporting Query Results as XML 
    10.43 Importing XML into MySQL以下附上书中的脚本:xml_to_mysql.pl#! /usr/bin/perl -w
    # xml_to_mysql.pl - read XML file into MySQLuse strict;
    use DBI;
    use XML::XPath;# ... process command-line options (not shown) ...# ... connect to database (not shown) ...# Open file for reading
    my $xp = XML::XPath->new (filename => $file_name);
    my $row_list = $xp->find ("//row");         # find set of <row> elements
    print "Number of records: " . $row_list->size ( ) . "\n";
    foreach my $row ($row_list->get_nodelist ( ))        # loop through rows
    {
        my @name;   # array for column names
        my @val;    # array for column values
        my $col_list = $row->find ("*");                # children (columns) of row
        foreach my $col ($col_list->get_nodelist ( ))    # loop through columns
        {
            # save column name and value
            push (@name, $col->getName ( ));
            push (@val, $col->string_value ( ));
        }
        # construct INSERT statement, then execute it
        my $stmt = "INSERT INTO $tbl_name ("
                    . join (",", @name)
                    . ") VALUES ("
                    . join (",", ("?") x scalar (@val))
                    . ")";
        $dbh->do ($stmt, undef, @val);
    }$dbh->disconnect ();