Reputation: 1515
I have a MYSQL database with 50.000 rows. Each row represents an article. I want the value of the column with they name "articletext" to be split into 50.000 files. One file for each row. I'm new to MYSQL so I'm not sure how to do this.
Can anyone help me?
Thanks
Upvotes: 2
Views: 1500
Reputation: 23351
And I propose my solution using 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'))
where row[0]
will map to id
, row[1]
will map to description
Upvotes: 2
Reputation: 1515
I created this small java application to solve the problem.
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();
}
Upvotes: 2