2017-02-14 7 views
0

MySQLを使用してSQLテーブルにデータをインポートしようとしています。しかし、テキストファイルのデータは別の方法で配置されます。そのような例が1つあります。テキストファイルをmysqlテーブルにインポートする

#n O. Willum 
#a Res. Center for Microperipherik, Technische Univ. Berlin, Germany 
#pc 1 
#cn 0 
#hi 0 
#pi 0.0000 
#upi 0.0000 
#t new product;product group;active product;long product lifetime;old product;product generation;new technology;environmental benefit;environmental choice;environmental consequence 

#index 2 
#n D. Wei 
#a Dept. of Electr. & Comput. Eng., Drexel Univ., Philadelphia, PA, USA 
#pc 1 
#cn 0 
#hi 0 
#pi 0.0000 
#upi 0.0000 
#t lowpass filter;multidimensional product filter;orthonormal filterbanks;product filter;new approach;novel approach;challenging problem;iterative quadratic programming;negligible reconstruction error;spectral factorization 

このテキストファイルをSQLファイルに変換するにはどうすればよいですか?

+0

あなたはおそらくより容易にロード可能な形式にデータを変換するために別のプログラムを使用する必要があるの末尾に改行を持っています。 'LOAD DATA INFILE'クエリを使用することはできません。なぜなら、列名と列データを区切るためにスペースが使用されているためです。**および**列データ内にエスケープされていないスペースが埋め込まれています。 – onlynone

答えて

0

少しAWKスクリプトでテストできます。 tablename 2xを変更し、フィールド名を実際のものに置き換えてください。出力をファイルにリダイレクトし、mysqlクライアントで直接実行することができます。

awkスクリプト

bash-3.2$ cat gen.awk 
BEGIN { 
     FS=" " 
     print "-- createt by gen.awk" 
     print "-- written by Bernd Buffen" 
     print "-- [email protected]" 
     print 
     out="INSERT INTO myTable SET " 
     ok=0; 
} 
{ 
     if($1 != "") { rem="-- " 
     for(i=1;i<=NF;i++) 
     { 
      rem=rem" "$i 
     } 
     } 
     print rem; 



     if($1 == "#n" ) {out=out""sep"fieldname_n='"; ok=1;sep=", "; } 
     if($1 == "#a" ) {out=out""sep"fieldname_a='"; ok=1;sep=", "; } 
     if($1 == "#pc") {out=out""sep"fieldname_pc='"; ok=1;sep=", "; } 
     if($1 == "#cn") {out=out""sep"fieldname_cn='"; ok=1;sep=", "; } 
     if($1 == "#hi") {out=out""sep"fieldname_hi='"; ok=1;sep=", "; } 
     if($1 == "#pi") {out=out""sep"fieldname_pi='"; ok=1;sep=", "; } 
     if($1 == "#upi") {out=out""sep"fieldname_upi='"; ok=1;sep=", "; } 
     if($1 == "#t" ) {out=out""sep"fieldname_t='"; ok=1;sep=", "; } 
     if(ok == 1) 
     { 
      tmp=$2 
      for(i=3;i<=NF;i++) 
      { 
       tmp=tmp" "$i 
      }; 
      out=out""tmp"' "; 
      ok=0; 
     } 

     if($1 == "") 
     { 
      print out";" 
      print; 
      out=""; 
      sep=""; 
      out="INSERT INTO myTable SET "; 
      ok=0; 
     } 
} 

入力ファイル

それなければなりません!ファイル

bash-3.2$ cat text.txt 
#n O. Willum 
#a Res. Center for Microperipherik, Technische Univ. Berlin, Germany 
#pc 1 
#cn 0 
#hi 0 
#pi 0.0000 
#upi 0.0000 
#t new product;product group;active product;long product lifetime;old product;product generation;new technology;environmental benefit;environmental choice;environmental consequence 

#index 2 
#n D. Wei 
#a Dept. of Electr. & Comput. Eng., Drexel Univ., Philadelphia, PA, USA 
#pc 1 
#cn 0 
#hi 0 
#pi 0.0000 
#upi 0.0000 
#t lowpass filter;multidimensional product filter;orthonormal filterbanks;product filter;new approach;novel approach;challenging problem;iterative quadratic programming;negligible reconstruction error;spectral factorization 

出力

bash-3.2$ awk -f gen.awk text.txt 
-- createt by gen.awk 
-- written by Bernd Buffen 
-- [email protected] 

-- #n O. Willum 
-- #a Res. Center for Microperipherik, Technische Univ. Berlin, Germany 
-- #pc 1 
-- #cn 0 
-- #hi 0 
-- #pi 0.0000 
-- #upi 0.0000 
-- #t new product;product group;active product;long product lifetime;old product;product generation;new technology;environmental benefit;environmental choice;environmental consequence 
-- #t new product;product group;active product;long product lifetime;old product;product generation;new technology;environmental benefit;environmental choice;environmental consequence 
INSERT INTO myTable SET fieldname_n='O. Willum' , fieldname_a='Res. Center for Microperipherik, Technische Univ. Berlin, Germany' , fieldname_pc='1' , fieldname_cn='0' , fieldname_hi='0' , fieldname_pi='0.0000' , fieldname_upi='0.0000' , fieldname_t='new product;product group;active product;long product lifetime;old product;product generation;new technology;environmental benefit;environmental choice;environmental consequence' ; 

-- #index 2 
-- #n D. Wei 
-- #a Dept. of Electr. & Comput. Eng., Drexel Univ., Philadelphia, PA, USA 
-- #pc 1 
-- #cn 0 
-- #hi 0 
-- #pi 0.0000 
-- #upi 0.0000 
-- #t lowpass filter;multidimensional product filter;orthonormal filterbanks;product filter;new approach;novel approach;challenging problem;iterative quadratic programming;negligible reconstruction error;spectral factorization 
-- #t lowpass filter;multidimensional product filter;orthonormal filterbanks;product filter;new approach;novel approach;challenging problem;iterative quadratic programming;negligible reconstruction error;spectral factorization 
INSERT INTO myTable SET fieldname_n='D. Wei' , fieldname_a='Dept. of Electr. & Comput. Eng., Drexel Univ., Philadelphia, PA, USA' , fieldname_pc='1' , fieldname_cn='0' , fieldname_hi='0' , fieldname_pi='0.0000' , fieldname_upi='0.0000' , fieldname_t='lowpass filter;multidimensional product filter;orthonormal filterbanks;product filter;new approach;novel approach;challenging problem;iterative quadratic programming;negligible reconstruction error;spectral factorization' ; 

bash-3.2$ 
関連する問題