Encoding issue with mysql when using UCS2 - mysql

I am trying to import one big csv file encoded in UCS2-Big Indian into a mysql table.
This is the mysql code:
DROP TABLE if exists PAPERS;
CREATE TABLE `PAPERS` (
ID_RESEARCHER VARCHAR(20),
PAPER_ACCESSOR_NUMBER VARCHAR(20),
primary key(ID_RESEARCHER,PAPER_ACCESSOR_NUMBER)
) ENGINE=InnoDB DEFAULT CHARSET=ucs2;
load data local infile '...dump_all_papers_test_2.csv'
into table PAPERS
CHARACTER SET ucs2
fields terminated by '\t' enclosed by '"'
lines terminated by '\n'
(ID_RESEARCHER, PAPER_ACCESSOR_NUMBER);
And the content of csv (format ucs-2 Big Indian, said by notepad++)
"100" "A1974U626600001"
"100" "A1974U626600001"
"100" "A1974U626600001"
A copy of csv sample:
http://pastebin.com/HMssuxCf
And the error is:
1 row(s) affected, 2 warning(s):
1265 Data truncated for column 'ID_RESEARCHER' at row 1
1261 Row 1 doesn't contain data for all columns
Records: 1 Deleted: 0 Skipped: 0 Warnings: 2
What is happening here?
The action only fills the first field. Is not mysql support ucs2?

Ok.
The solution is in the documentation:
Note that it is currently not possible to load data files that use the ucs2 character set.
http://dev.mysql.com/doc/refman/5.0/en/load-data.html

Related

SQL Loading Data But Missing First Section

mysql> LOAD DATA LOCAL INFILE 'students.csv' INTO TABLE students FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' (studentid,fname,mname,lname,psuid,state,country,major);
Query OK, 997 rows affected, 2276 warnings (0.02 sec)
Records: 997 Deleted: 0 Skipped: 0 Warnings: 2276
students table image
After loading my data into the table my first part of the data was missing the first name is gone. Which is why the major section is empty and everything is shifted over.
Please help! Thank you :)
CSV image
I assumed studentid column is defined with AUTO_INCREMENT attribute. You should remove definition of studentid in your LOAD DATA INFILE statement and let MySQL auto generate it.
Try this:
LOAD DATA LOCAL INFILE 'students.csv'
INTO TABLE students
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n'
(fname,mname,lname,psuid,state,country,major);
Your data in the file does not seem to be in the correct order.

Escape and enclose by same symbol doesn't work

I have a table with a nullable column:
CREATE TABLE test (id INT, value INT);
INSERT INTO test VALUES (1, null);
I export it to csv using
SELECT * FROM test
INTO OUTFILE '/tmp/test.csv'
CHARACTER SET 'utf8'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
The output file looks like this:
1,"N
Then I try to import it:
LOAD DATA LOCAL INFILE '/tmp/test.csv'
INTO TABLE `test`.`test`
CHARACTER SET 'utf8'
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"';
Query OK, 1 row affected, 1 warning (0.01 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 1
And it does't work and generates the following warning:
SHOW WARNINGS;
Warning 1366: Incorrect integer value: '"N' for column 'value' at row 1
And it doesn't insert this row. For some reason escaping doesn't work for this case. It does work if field are escaped and enclosed by different symbols.
But I can't use different symbols because I can't change export query because I use Google Cloud SQL and export to csv can be done only from google web console without ability to edit the query.
I've seen multiple examples on the internet that double quote in both cases should work. Am I doing something wrong?

LOAD FILE INTO TABLE...0 rows affected

I have tried to load a .csv file into mysql in many different ways. I have most recently tried
load data local infile 'c:\Users\...csv' into table suppression_list;
which works, but I'm told 0 rows affected.
I've tried to drop the local but then it says the file is not found. I've tried to add lines terminated by ... and fields terminated by ... but that doesn't seem to make any difference. I've also tried saving the file as .txt but with no luck. suppression_list is just one column, email text.
Here is a snapshot of my data
覧覧覧覧覧
覧覧覧覧覧
#
# Contacts
*****#une.net.co
.australia#gmail.com
.d.yncly.d.ay#gmail.com
.dy.n.cly.d.a.y#gmail.com
.je.n.i.s.di.ken.s.o.n.1#gmail.com
.jell.yhe.a.d1.2.12#gmail.com
You can see it's just one column of email addresses, some of which look a little funky, I'll admit.
Do a
LOAD DATA INFILE '...csv'
INTO TABLE myTable
LINES TERMINATED BY '\n'
IGNORE 4 LINES
to ignore the Header lines. If you have created mytable like
CREATE TABLE mytable (
`email` VARCHAR(255) NULL
)
This results in
6 row(s) affected Records: 6 Deleted: 0 Skipped: 0 Warnings: 0 0.000 sec

SQL - LOAD DATA INFILE Error

I am trying to load data from a CSV into a database in MySQL workbench. The table I am loading into has an auto increment ID column. I am trying to get the query to recognize that I want it to keep the first column as Null, and I put NULL as the value in the CSV, but I cannot get the SET ... NULL command to recognize the name of the ID column. Here is the SQL I am using:
load data infile 'filenam.csv'
INTO TABLE table_name
fields Terminated By ','
LINES TERMINATED BY ',,'
SET column_name = null
I suspect I am making a simple syntax error that is causing the problem. But I cannot figure out what it is.
If you put NULL as the value in the CSV file then you shouldn't need the "SET column_name = null" in the statement. AFAIK, the SET value should be used to supply values not derived from the input file or to perform calculations to the value before insertion. This statement should work fine since you said you specified NULL in the CSV. However, make sure you specified NULL "properly" according to the documentation. I always use \N in my import files.
LOAD DATA INFILE 'filename.csv'
INTO TABLE table_name
FIELDS TERMINATED BY ','
LINES TERMINATED BY ',,'
Secondly, you can discard the NULL specified in the CSV file by assigning it to a user variable and then specifying the column value with SET. However, you need to specify a column list
LOAD DATA INFILE 'filename.csv'
INTO TABLE table_name (#dummy, column_2, column_3)
FIELDS TERMINATED BY ','
LINES TERMINATED BY ',,'
SET column_name = NULL
I have one other thought based on the MySQL docs and dependent upon how your server is configured. Comment if this does not work and I will provide more options.
http://dev.mysql.com/doc/refman/5.1/en/load-data.html

MySQL "load data local infile" just loads 1 row

I need to upload a csv file to my MySQL table. For some reason, when I run the command below in Mac Terminal, just one row is inserted. How can I upload all the rows?
load data local infile 'Research/cube_sample_data.csv'
into table ad_spend
fields terminated by ','
lines terminated by '\n';
I tried '\r\n' and ',\r\n' but no success. I keep getting this message:
Query OK, 1 row affected, 0 warnings (0.00 sec)
Records: 1 Deleted: 0 Skipped: 0 Warnings: 0
my file is like this:
entertainment, games, 14
entertainment, movies, 12
and I have an empty table with 3 columns:
category (varchar), subcat (varchar), adspend (int)
It seems that there's not only a comma between fields, but also a whitespace. So try to use:
fields terminated by ', '