Export a SQL database into a CSV file and use it with WEKA

心已入冬 提交于 2019-12-23 02:48:19

问题


How can I export a query result from a .sql database into a .csv file? I tried with

SELECT * 
FROM players 
  INTO OUTFILE 'players.csv' 
  FIELDS TERMINATED BY ',' 
  LINES TERMINATED BY ';';` 

and my .csv file is something like:

p1,1,2,3
p2,1,4,5

But they are not in saparated columns, all are in 1 column.

I tried to create a .csv file by myself just to try WEKA, something like:

p1  1  2  3
p2  1  4  5

But WEKA recognizes p1 1 2 3 as a single attribute.

So: how can I export correctly a table from a sql db to a csv file? And how can I use it with WEKA?

EDIT

Is it possible to add a header line with columns names? I have read something online but I am not able.



I'm using MySQL 5.5.


回答1:


You need to inform weka about format of your csv file. You can do so while opening csv file and invoking options dialog, see below images. Also you need to terminate your lines with "\n" instead of ";".




回答2:


Try this.

First of all convert the .sql file to .csv format. For eg:

SELECT orderNumber, status, orderDate, requiredDate, comments 
FROM orders
INTO OUTFILE 'C:/tmp/cancelled_orders.csv'
FIELDS ENCLOSED BY '"' TERMINATED BY ',' ESCAPED BY '"'
LINES TERMINATED BY '\r\n';

NOTE: Weka tool need the .csv file`s attributes terminated by a comma. If you use semi colon, it wont work. You will get an exception.

Now this will convert the .sql format to .csv format which you can find in the C:/tmp/cancelled_orders.csv location.

Finally start the weka tool and fetch this data set to it.

NOTE : Make the "Files of Type" as "CSV data files".

Then it will work without giving any exception. It works.



来源:https://stackoverflow.com/questions/12438743/export-a-sql-database-into-a-csv-file-and-use-it-with-weka

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!