Search
 
SCRIPT & CODE EXAMPLE
 

SQL

Export Data From Database to CSV File

SELECT column_a,column_b,column_c+column_d INTO OUTFILE '/tmp/result.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '
'
FROM test_table;
Comment

Export Database Records to CSV

<?php
$conn = mysqli_connect("localhost","root","test", "phppot_examples");

$query = "SELECT * FROM toy";
$result = mysqli_query($conn, $query);

$num_column = mysqli_num_fields($result);		

$csv_header = '';
for($i=0;$i<$num_column;$i++) {
    $csv_header .= '"' . mysqli_fetch_field_direct($result,$i)->name . '",';
}	
$csv_header .= "
";

$csv_row ='';
while($row = mysqli_fetch_row($result)) {
	for($i=0;$i<$num_column;$i++) {
		$csv_row .= '"' . $row[$i] . '",';
	}
	$csv_row .= "
";
}	

/* Download as CSV File */
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=toy_csv.csv');
echo $csv_header . $csv_row;
exit;
?>
Comment

How to automatically export database to a csv file

sqlcmd -S . -d DatabaseName -E -s, -W -Q "SELECT * FROM TableName" > C:Test.csv
Comment

Export Data From Database to CSV File

SET @TS = DATE_FORMAT(NOW(),'_%Y_%m_%d_%H_%i_%s');
SET @FOLDER = '/var/lib/sql-files/';
SET @PREFIX = 'employees';
SET @EXT    = '.csv';
SET @CMD = CONCAT("SELECT * FROM tableName INTO OUTFILE '",@FOLDER,@PREFIX,@TS,@EXT,
"' FIELDS ENCLOSED BY '"
' TERMINATED BY ',' 
ESCAPED BY '"'",
"LINES TERMINATED BY 'n';");
PREPARE statement FROM @CMD;
EXECUTE statement;
Comment

PREVIOUS NEXT
Code Example
Sql :: mysql --version 
Sql :: index in mysql 
Sql :: sorting desc in sql 
Sql :: sql output parameters 
Sql :: aggregate functions 
Sql :: sql server concat null 
Sql :: store date time in mysql 
Sql :: 3rd height salary sql 
Sql :: sqlite3.OperationalError: near "7": syntax error 
Sql :: ranking functions in sql 
Sql :: sql server port 1434 
Sql :: oracle alter table 
Sql :: how to use sqlcommand 
Sql :: sql basic commands 
Sql :: constraint sql 
Sql :: mov volume before build 
Sql :: use table postgres 
Sql :: app times 
Sql :: like sql for second letter of the family name 
Sql :: use mysql 8 without password 
Sql :: convert linq to sql query online 
Sql :: fetching data from mysqldb 
Sql :: db2 foreign keys 
Sql :: cassandra query example 
Sql :: Is there a way to use read_sql_query and the query has WHERE column = Variable? 
Sql :: update top 100 order by sql server 
Sql :: knex sqlite 
Sql :: What is the library that should import to use all functional database features in SQLite? 
Sql :: python sql passer des parametres dans une requet pymysql 
Sql :: Fatal error: Uncaught mysqli_sql_exception: Unknown or incorrect time zone 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =