In one of my rails action i want to create a csv file from a table using MySql OUTFILE.
path = "#{Rails.root}/public/outfile.csv"
query_string = "SELECT * INTO OUTFILE '#{path}' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' FROM temp_csv_186;"
ActiveRecord::Base.connection.execute(query_string)
But every time its showing the error.
Mysql2::Error: Can't create/write to file '/home/user/Projects/Application/public/outfile.csv' (Errcode: 13): SELECT * INTO OUTFILE '/home/user/Projects/Application/public/outfile.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' FROM temp_csv_186;
Has whichever user Mysql runs under got write permissions for /home/user/Projects/Application/public/ ?
Related
SELECT * INTO OUTFILE 'dataset.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '""' LINES TERMINATED BY '\n' FROM reviews.opinions;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
The beloved my perl script code,
#!/usr/bin/perl -w
..
.// db connection
..
$sth=$dbh->prepare(" SELECT * FROM Table_nm ") or warn $dbh->errstr;
$sth->execute or die "can't execute the query: $sth1->errstr";
// I want to export the result of mysql query into csv file.
Please help. Thanx!
Unless you need to process the result set prior to outputting it to a csv file, then it would be more efficient to export it directly by adjusting the sql statement.
SELECT a,b,a+b INTO OUTFILE '/tmp/result.txt'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
FROM test_table;
13.2.9.1 SELECT ... INTO Syntax
EDIT
To be able to get the header line, you need to add a second select statement which specifies the fields and union that with the other select statement.
Example:
mysql> select 'OTOBJID','OTTRANSID'
union
select * into outfile 'd:/test.csv'
fields terminated by ','
optionally enclosed by '"'
from objecttransports;
D:\>type test.csv
"OTOBJID","OTTRANSID"
"0","0"
"0","1"
"0","2"
"0","3"
"0","4"
"0","5"
"1","0"
"1","1"
"1","2"
"1","4"
"1","5"
I am trying to load a few thousand records into my MySQL db from a tab delimited text file, but I am getting the error message:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'FIELDS
TERMINATED BY '\t' ENCLOSED BY '" '
LINES TERMINATED BY '\n'' at line 2
My command is:
LOAD DATA INFILE 'records.txt' INTO TABLE records (vendor, title, id, part, project,
description, machine, shelf, compartment, checkout)
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n';
I have tried different options such as OPTIONALLY ENCLOSED BY '"', LINES TERMINATED BY '\r\n', and adding a space after the quotation mark in ENCLOSED BY '" ', but I still get the error message above.
Where am I going wrong?
I am not very used to this command but I would say that the right query is :
LOAD DATA INFILE 'records.txt' INTO TABLE records
FIELDS TERMINATED BY '\t' ENCLOSED BY '"'
LINES TERMINATED BY '\n' (vendor, title, id, part, project,
description, machine, shelf, compartment, checkout);
Hi I am trying to load data from a file to Mysql DB using hibernate.
here is the query,
session.createSQLQuery("LOAD DATA INFILE E:/uploaded/NumSerie/NS/NumSerie.txt INTO TABLE prod CHARACTER SET latin1 FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;").executeUpdate();
But i get the following error,
org.hibernate.QueryException: Space is not allowed after parameter prefix ':' [LOAD DATA INFILE E:/uploaded/NumSerie/NS/NumSerie.txt INTO TABLE prod CHARACTER SET latin1 FIELDS TERMINATED BY ';' LINES TERMINATED BY '
' IGNORE 1 LINES;]
at org.hibernate.engine.query.ParameterParser.parse(ParameterParser.java:92)
at org.hibernate.engine.query.ParamLocationRecognizer.parseLocations(ParamLocationRecognizer.java:75)
How can I rewrite this query so this is executed properly?
Thanks in advance!
Try creating a parameterised query
I'm no Hibernate guru but this could work:
session.createSQLQuery("LOAD DATA INFILE :file INTO TABLE prod CHARACTER SET latin1 FIELDS TERMINATED BY ';' LINES TERMINATED BY '\n' IGNORE 1 LINES;")
.setString("file", "E:/uploaded/NumSerie/NS/NumSerie.txt")
.executeUpdate();
Consider this code:
mysql> select * into outfile 'atmout12.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from atm_atm;
ERROR 1086 (HY000): File 'atmout12.csv' already exists
mysql> select * into outfile 'atmout1.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' from atm_atm;
Query OK, 2822 rows affected (0.02 sec)
I used the above snippet to convert a table data to a CSV file. As you can see the query ran fine, but I am unable to locate where the file is.
I do an ls in the folder and can't locate it. I am using Ubuntu 11.04
The file will be locate in your data directory.
example: datadir=/opt/data/db_name.
Inside the particular database (db_name) folder/dir will contain your .csv file.
OR else we can give the output file in particular location , to generate like that the user should have super privileges.
example :
mysql > use db_name
mysql> select * into outfile 'atmout1.csv' from atm_atm;
or
mysql> select * into outfile '/opt/example.csv' from atm_atm;
NOTE :above output file will be in db_name folder.