Using LOAD DATA LOCAL INFLE for reading XML files - mysql

I know using LOAD XML LOCAL INFILE for reading XML files.
but i do not need to separate xml elements. I like to import whole xml file as a text.
Is it possible reading whole XML file as a text file?
I used this command
LOAD DATA LOCAL INFILE 'file.xml' INTO TABLE mytable;
the result is:
The query has been successfully implemented, 150 rows have been
affected.
But when i use SELECT * FROM mytable; it shows 150 empty lines!

Use this:
LOAD DATA LOCAL INFILE 'file.xml' INTO TABLE mytable
FIELDS TERMINATED BY '\n' LINES STARTING BY '';
just replace FIELDS TERMINATED BY value & LINES STARTING BY value as per your file.

Related

Trouble loading data from txt file into MySQL with LOAD DATA INFILE

I am having a hard time loading my data to MySQL from a text file. I have been attempting to choose the correct delimiters but my file contains column names with each value.
The data is structured like this
{"id":"15","name":"greg","age":"32"}
{"id":"16","name":"jim","age":"42"}
the sql statement I am working on looks something like this currently
LOAD DATA LOCAL INFILE '/xxx.txt' INTO TABLE t1 FIELDS
TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\r\n'(id, name, age);
results are being stored like this
{"id":"16", "name","greg"}
I need to do away with the column name and store the value.
any tips?
Writing a script as suggested by #Shadow will be easier to wrap around but you can check the section on json on this page how to import json-text-xml and csv data into mysql
or
Import JSON to MySQL made easy with the MySQL Shell if you are using MySQL Shell 8.0.13 (GA)

How to Load amount using LOAD DATA LOCAL INFILE?

i'm unable to load amount using LOAD DATA LOCAL INFILE
I have a text file where there is one column called 'Amount'. My problem is this field has values as 20,200.00 and -61,066.11 etc in the text file. When I try to load these values in my MySQL database, it loads only 20 and -61.
What should I do to load the full amount in my table?
I am using Decimal(10,2) data type.
I am using following query.
LOAD DATA LOCAL INFILE 'C:/Users/Dataction123/Desktop/nordic.txt' INTO TABLE spend_nordic LINES TERMINATED BY '\r\n';
LOAD DATA LOCAL INFILE 'C:/Users/Dataction123/Desktop/nordic.txt' IGNORE
INTO TABLE spend_nordic
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\r\n'
(int_col, #float_col)
SET float_col = replace(#float_col, ',', '.');
Explanation :
MySQL casts the value into the column type when it reads it from the file, before applying the transformations described in the SET clause. If you instruct it to read it into a variable the value is handled as string from the beginning. float_col is name of field.
For reference Click here

load data infile syntax in mysql

I am using LOAD DATA local INFILE command to upload big csv file in mysql db. The field is an integer in the mysql table and so it will accept only integer.
Now if the csv file contains numbers like below then the sql doesn't work.The format of my csv file is
ID
"322"
"445"
"211"
So it is inserting 0 in the table. But if the csv file contains no double quotes then it's fine.
How can I tell "load data infile" command to ignore double quotes( as sometimes it is too difficult to remove double quotes from csv file which has got about 1 million records).My Sql is
$insert_query = "LOAD DATA local INFILE '".$target_path."'
INTO table master_huts
IGNORE 1 LINES
(hids)
";
Try adding this:
FIELDS OPTIONALLY ENCLOSED BY '"'
before:
IGNORE 1 LINES

can LOAD DATA INFILE use a giving path instead of the default mysql data path?

I am trying to use LOAD DATA INFILE to upload xls/CSVfile content into mysql database.
the file in which I want to upload is located on a different path that the databases. I don't want to put the xls/CSV file in my same path as my data so I put them into a different path. but it seems that MySQL ignore the path I am providing It always searching for a file located in the data bases directory.
here is my code
LOAD DATA INFILE 'D:\uploads\new_campaign_request.xls'
INTO TABLE new_db.new_campaign
FIELDS TERMINATED BY ',';
How can I get MySQL to search for the file
LOAD DATA INFILE 'D:\\uploads\\new_campaign_request.xls'
INTO TABLE new_db.new_campaign(column_name)
FIELDS TERMINATED BY ',';
User double back slash for load file path and define column name in which data should be loaded

Import txt file into my database with specific format

I am new to MySQL and I searched internet. Before I used CSV file or SQL file to import my data into DB. But now I have a text file with this format:
number
989399999999
989388888888
989384444444
This continues about 300 records. How can I import this to my database?
LOAD DATA INFILE
The LOAD DATA INFILE statement reads rows from a text file into a
table at a very high speed. The file name must be given as a literal
string.
LOAD DATA LOCAL INFILE 'C:\\Users\\hi\\Desktop\\abc.txt' INTO TABLE customer LINES TERMINATED BY '\n' IGNORE 1 LINES
Query is self explanatory I hope :)