2
我有一個有50.000行的MYSQL數據庫。每一行代表一篇文章。我想要將他們名爲「articletext」的列的值分割爲50.000個文件。每行一個文件。我是MYSQL的新手,所以我不知道該怎麼做。將MYSQL行導出到幾個txt文件中
任何人都可以幫助我嗎?
謝謝
我有一個有50.000行的MYSQL數據庫。每一行代表一篇文章。我想要將他們名爲「articletext」的列的值分割爲50.000個文件。每行一個文件。我是MYSQL的新手,所以我不知道該怎麼做。將MYSQL行導出到幾個txt文件中
任何人都可以幫助我嗎?
謝謝
我創建了這個小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();
}
我建議使用Python我的解決方案:
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[0]
將映射到id
,row[1]
將映射到description
這大概不能與MySQL獨自完成。你能用一種編程語言嗎? – 2012-03-18 14:09:48
感謝您的回覆。我可以創建一個連接到數據庫的小型Java應用程序。這聽起來像是最好的解決方案嗎? – Peter 2012-03-18 14:12:48
你肯定會需要另一種語言來完成程序部分--sql不是一種文件創建語言,它是一種查詢語言。 – Randy 2012-03-18 14:12:54