2012-03-18 6 views
2

50.000 행의 MYSQL 데이터베이스가 있습니다. 각 행은 기사를 나타냅니다. "articletext"라는 이름의 열 값을 50.000 개 파일로 나누고 싶습니다. 각 행에 대해 하나의 파일. 나는 MYSQL을 처음 사용하므로 어떻게해야할지 모르겠다.MYSQL 행을 여러 txt 파일로 내보내기

아무도 도와 줄 수 있습니까?

감사합니다.

+1

에 매핑됩니다. 프로그래밍 언어를 사용할 수 있습니까? –

+0

답장을 보내 주셔서 감사합니다. 데이터베이스에 연결하는 작은 Java 응용 프로그램을 만들 수 있습니다. 그게 가장 좋은 솔루션 같습니까? – Peter

+0

절차 적 부분을 수행하는 데 확실히 다른 언어가 필요합니다. SQL은 파일 생성 언어가 아니며 쿼리 언어입니다. – Randy

답변

2

문제를 해결하기 위해이 작은 Java 응용 프로그램을 만들었습니다.

 try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     System.out.println("Opening connection"); 
     Connection con = DriverManager.getConnection(
       "jdbc:mysql://localhost/articles", "username", "password"); 

     String query = "Select title,articletext from articles"; 

     Statement stmt = con.createStatement(); 
     ResultSet rs = stmt.executeQuery(query); 

     while (rs.next()) { 
      String title = rs.getString(1); 
      String text = rs.getString(2); 

      try { 
       FileWriter fstream = new FileWriter(title + ".txt"); 

       BufferedWriter out = new BufferedWriter(fstream); 
       out.write(text); 
       out.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 

     System.out.println("Closing connection"); 
     con.close(); 

    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
2

그리고 파이썬을 사용하여 내 솔루션 제안 : row[0]id 매핑됩니다

import MySQLdb 

def write_to_file(with_name, write_this): 
    with_name = str(with_name) 
    with open(with_name, "w") as F: 
     F.write(write_this) 

db = MySQLdb.connect(
    host="localhost", # hostname, usually localhost 
    user="andi",   # your username 
    passwd="passsword", # your password 
    db="db_name",  # name of the database 
    charset = "utf8", # encoding 
    use_unicode = True 
) 

cur = db.cursor() 

cur.execute("select id, description from SOME_TABLE") 

for row in cur.fetchall() : 
    write_to_file(row[0], row[1].encode('utf8')) 

row[1]description이 아마 혼자 MySQL을 수행 할 수 없습니다

관련 문제