日期:2014-05-16  浏览次数:20786 次

mysqlimport运用
格式:mysqlimport [options] db_name textfile1 [textfile2 ...]

例子:mysqlimport -u username -ppassword db_name c:/grid_card.txt -L -Delete --fields-terminated-by=,

注意:textfile1的文件名第一点之前的名称必须与db_name数据库中的表名一致。此数据将导入该表中。
导入的文件:只包含要导入的值。

mysqlimport 命令会调用LOAD DATA导入数据。
可以参照:LOAD DATA

mysql> LOAD DATA INFILE "load_gridCard.sql" INTO TABLE grid_card FIELDS TERMINAT

ED BY ',' LINES TERMINATED BY '\n';

表的类型为MyISAM比InnDB快很多。

mysqlimport的具体格式请参照:
http://linux.cn/home/space-3872-do-thread-id-984.html

在java里调用该命令:
public static String importTableData (String tableName) {

       long startTime = System.currentTimeMillis();

       StringBuilder sb = new StringBuilder();

       Runtime rt = Runtime.getRuntime();

       String com = "cmd.exe /c \"E:/Program Files/mysql-5.5.8-win32/bin/mysqlimport\" -u username -ppassword dbName c:/"

           + tableName + ".txt --delete -L --fields-terminated-by=,";

       try {

           Process p = rt.exec(com);

           BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

           String line = null;

           while((line = br.readLine()) != null) {

              sb.append(line);

           }
           br = new BufferedReader(new InputStreamReader(p.getErrorStream()));

           line = null;

           while((line = br.readLine()) != null) {

              sb.append(line);

           }

           if (p != null)

              p.destroy();

 

       } catch (IOException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

      

       long endTime = System.currentTimeMillis();

      

       System.out.println("load data to <" + tableName + "> use time : " + (endTime-startTime));

      

       return sb.toString();

    }