将MYSQL行导出到多个txt文件中
我有一个MYSQL数据库,有50000行。每行代表一篇文章。我希望将名为“articletext”的列的值拆分为50000个文件。每行一个文件。我是MYSQL的新手,所以我不知道该怎么做 有人能帮我吗将MYSQL行导出到多个txt文件中,mysql,select,Mysql,Select,我有一个MYSQL数据库,有50000行。每行代表一篇文章。我希望将名为“articletext”的列的值拆分为50000个文件。每行一个文件。我是MYSQL的新手,所以我不知道该怎么做 有人能帮我吗 谢谢我创建了这个小型java应用程序来解决这个问题 try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("Opening connection"); Con
谢谢我创建了这个小型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();
}
我创建了这个小型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'))
其中
行[0]
将映射到id
,行[1]
将映射到说明
,我建议使用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'))
其中,
行[0]
将映射到id
,行[1]
将映射到描述
,这可能无法单独使用mySQL完成。你会使用编程语言吗?谢谢你的回复。我可以创建一个连接到数据库的小型java应用程序。这听起来像是最好的解决方案吗?您肯定需要另一种语言来完成过程部分-sql不是一种文件创建语言,而是一种查询语言。是的@Peter,您可以使用java。连接到数据库,编写一个select语句来循环50000行,创建一个文件对象,并为每行写入值。这可能无法仅用mySQL来完成。你会使用编程语言吗?谢谢你的回复。我可以创建一个连接到数据库的小型java应用程序。这听起来像是最好的解决方案吗?您肯定需要另一种语言来完成过程部分-sql不是一种文件创建语言,而是一种查询语言。是的@Peter,您可以使用java。连接到数据库,编写select语句以循环50000行,创建一个文件对象,并将每行的值写入其中。