LOAD DATA INFILE To Import CSV File Into Database Table - php

As a workaround, I am trying to use this syntax to import my csv file into my database table instead of fgetcsv because files uploaded may be quite big. So I made this:
$connect = mysql_connect("localhost","root","");
mysql_select_db("dbtest",$connect);
$filename = $file_path;
$sql = "LOAD DATA INFILE '$filename'
INTO TABLE tbltest
FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n'
(#rcode, #pcode, #mcode, #bcode, #ecode, #filetype, #rec_count, #ges_count, #ck_count, #ic_count, #ran_count);" ;
//echo $sql;
mysql_query ($sql,$connect) ;
Now, the problem is nothing is inserted in my table. When I do echo $sql, the result is this:
LOAD DATA INFILE 'uploads/testfile.csv' INTO TABLE tbltest FIELDS TERMINATED BY '|'
LINES TERMINATED BY '\n' (#rcode, #pcode, #mcode, #bcode, #ecode, #filetype, #rec_count,
#ges_count, #ck_count, #ic_count, #ran_count);
The testfile.csv looks like this:
01|01|01|01|0000|FORM1|5|5|5|||||||||
01|01|01|01|0000|FORM2|238|238||||||||||
01|01|01|01|0000|FORM4|0|||||||||||
01|01|01|01|0000|FORM5|1|||||||||||
01|01|01|01|0000|FORM3|3|3||||||||||

Related

how to add custom column in db table when using 'into outfile' query

I am using this query to create CSV of my mysql DB table
$path = getcwd() . '/uploads/data.csv' ;
$sql = "select email,username from `engine4_user` LIMIT 5000 into outfile '$path' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ";
But now I need one extra field that is profile url .which doesn't exists on the table. So i wanna create a virtual url column using the ID of that user table. Like https://mysiteurl/profile/$user_id .
Please help. Thanks in advance.
Use "CONCAT" operator
$sql = "SELECT
email,
username,
CONCAT('https://mysiteurl/profile/',user_id) as profile_url
FROM `engine4_user`
LIMIT 5000
into outfile '$path' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' ";
See example here

How to run Multiple Queries and result store in multiple csv files

how to run mysql query and output the results in csv. i know the solution should be like this:
SELECT col1,col2,col3
FROM my_table
INTO OUTFILE '/tmp/orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
But my problem is,i have approx 15 queries and i want to run all 15 query and results store in multiple csv files.currently for first query it's working fine and create the csv file.but not creates the csv for other queries.Please Help :)
Query1
SELECT CE.ENTITY_ID 'CUSTOMER ID'
,CE.EMAIL 'CUSTOMER EMAIL ADDRESS'
,CE.CREATED_AT
,CE.UPDATED_AT
,CE.IS_ACTIVE
,CE.STORE_ID
,(SELECT VALUE
FROM CUSTOMER_ENTITY_VARCHAR CEV1
WHERE CEV1.ATTRIBUTE_ID = 1
AND CEV1.ENTITY_ID = CE.ENTITY_ID) AS 'FIRST NAME'
,CEV.VALUE AS 'LAST NAME'
FROM CUSTOMER_ENTITY CE, CUSTOMER_ENTITY_VARCHAR CEV
WHERE CE.ENTITY_ID = CEV.ENTITY_ID
AND CEV.ATTRIBUTE_ID = 2
INTO OUTFILE '$localDrive/DIM_CUSTOMER.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '`'
LINES TERMINATED BY '\n'";
Query2
SELECT LC.LOG_ID
,LC.VISITOR_ID
,LC.CUSTOMER_ID
,LC.LOGIN_AT
,LC.LOGOUT_AT
,LVI.HTTP_REFERER 'REFERRER SOURCE URL'
,LVI.HTTP_USER_AGENT
,LVI.HTTP_ACCEPT_LANGUAGE
,LVI.REMOTE_ADDR
FROM LOG_CUSTOMER LC, LOG_VISITOR_INFO LVI
WHERE LVI.VISITOR_ID = LC.VISITOR_ID
INTO OUTFILE '$localDrive/AGG CUSTOMER LOGIN.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '`'
LINES TERMINATED BY '\n'";
******************************************Solved***********************************************
Actual error is the way i execute the query.correct way is given below:
$db->exec($query)
that's it and it solve my problem:
Actual error is the way i execute the query.correct way is given below:
$db->exec($query);
that's it and it solve my problem:

how to insert last_insert_id in sql query of csv query?

i am importing csv data into mysql database using codegniter framework. my database has two table promotion and promotion_product.promotion table is 3 fields name as promotion_id,name,start_dateand end_date.promotion_id is the primary key of promotion table.promotion_id is the auto incremment.promotion_product table is 5 fields id,promotion_id,sku,price,map. promotion_id is the foreign key of this table. i am importing csv file this table.csv field is sku,price and map.i am not getting how to insert promotion_id of promotion_porduct table.i am using last_insert_id.my code is below:
my model is:
public function save_csv($data,$data1) {
$this->db->insert('promotion', $data1);
$insert_id = $this->db->insert_id();
print_r($insert_id);
$infile = $data['upload_data']['full_path'];
//print_r($infile);
$sql = "LOAD DATA INFILE '" . $infile . "'
INTO TABLE promotion_product
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r'
IGNORE 1 LINES
(promotion_id,sku, price, map)";
print_r($sql);
$query = $this->db->query($sql);
//var_dump($query);
return $query;
}
Yo have to do manually.
First insert into database then you will get last_insert_id and then read CSV file and create array of CSV and last_insert_id dump that array into a database.

MySQL LOAD DATA INFILE store line number

I'm using MySQL LOAD DATA INFILE to move CSV file data into a database table. I'm looking for a way to reference the original file line number (row) in the imported record.
So that a table like this:
CREATE TABLE tableName (
uniqueId INT UNSIGNED NOT NULL PRIMARY_KEY,
import_file VARCHAR(100),
import_line INT,
import_date = DATETIME,
fieldA VARCHAR(100),
fieldB VARCHAR(100),
fieldC VARCHAR(100)
);
Where import_file, import_line and import_date are meta data relevant to the specific file import. fieldA, fieldB and fieldC represent the actual data in the file.
Would be updated by a query like this:
LOAD DATA INFILE '$file'
REPLACE
INTO TABLE '$tableName'
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
LINES TERMINATES BY '\n'
IGNORE 1 LINES # first row is column headers
(fieldA,fieldB,fieldC)
SET import_date = now(),
import_file = '" . addslashes($file) . "',
import_line = '???';
Is there a variable I can set 'import_line' to?
Thanks,
-M
You can do that by setting a user variable first, and increment this variable in your SET clause, i.e.
SET #a:=0; -- initialize the line count
LOAD DATA INFILE 'c:/tools/import.csv' -- my test import
REPLACE
INTO TABLE tableName
FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES # first row is column headers
(fieldA,fieldB,fieldC)
SET import_date = now(),
import_file = 'import.csv',
import_line = #a:=#a+1; -- save the incremented line count

MySQL "LOAD LOCAL DATA INFILE"

I have hit a problem and i cant seem to get over it. I am taking information from a text file(two columns 29.12 23.42 for example separated by a space), using LOAD LOCAL DATA INFILE. Initially this was working with only one column in MySQL, however i have had to add an ID AUTO INCREMENT so i can use the most recent entry, in addition i want to now store the other column in my Database. Here is my code, its probably pretty obvious but I can't seem to find it:
<?PHP
$username = "root";
$password = "";
$hostname = "localhost";
$table = "Received_Data";
// Connect to Database and Table
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selectdatabase = mysql_select_db("IWEMSData",$dbhandle)
or die("Could not select IWEMSData");
echo "Connected to IWEMSData<br>";
// Clear Current Table and ReCreate
$dt = /*"UPDATE Received_Data SET PotVal = ''";*/ "DROP TABLE Received_Data";
mysql_query($dt);
$ctb = "CREATE TABLE Received_Data
(
Current DECIMAL(30,2) NOT NULL,
PotVal DECIMAL(30,2) NOT NULL,
ID BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID,PotVal,Current)
)";
mysql_query($ctb, $dbhandle);
echo "Received_Data Table has been created";
// Text File to read in
$Textfile = 'IWEMSData.txt';
mysql_query('
LOAD DATA LOCAL INFILE "IWEMSData.txt"
REPLACE INTO TABLE Received_Data
FIELDS TERMINATED BY ""
LINES TERMINATED BY "\\r\\n
(PotVal, Current)
";')
or die('Error Loading Data File.<br>' . mysql_error());
// Close Database connection when complete
mysql_close($dbhandle);
?>
So what i want is Column 1 is ID which will AUTO INCREMENT upon each entry. The second row is PotVal and the third row is Current. Then i want to only store in the second and third column.
The problem i am getting is that ID and Current are displaying the incorrect values and i can only seem to get one row.
Thanks in advance!
mysql_query('
LOAD DATA LOCAL INFILE "IWEMSData.txt"
REPLACE INTO TABLE Received_Data
FIELDS TERMINATED BY "" /*<- terminated by nothing? Shouldn't there be a space in it?*/
LINES TERMINATED BY "\\r\\n /*<- the closing " is missing*/
(PotVal, Current)
";') /*<- ah, here's the missing " What does it do here?*/
Write it like this:
mysql_query('
LOAD DATA LOCAL INFILE "IWEMSData.txt"
REPLACE INTO TABLE Received_Data
FIELDS TERMINATED BY " "
LINES TERMINATED BY "\\r\\n"
(PotVal, Current)
;')

Categories