Load data infile into specific column - mysql

I have a table (named "table_1") with only one column (named "col_1", which is an auto-increment primary key field). There are 100 rows in this column (filled with numbers 1 to 100).
The below code was used to create a new column named "col_2".
ALTER TABLE table_1
ADD COLUMN col_2 TINYINT;
I have a csv file with only one column and 100 rows, each row filled with one "0" or one "1", and I get an error message when I run the below code:
LOAD DATA INFILE 'path/to/file.csv'
INTO TABLE table_1
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n' (col_2);
Error code: 29. File 'path/to/file.csv' not found (OS errno 2 - No such file or directory)
I have already granted NETWORK_SERVICE full access to 'path/to/file.csv', but this problem persists.
Any help you can give would be greatly appreciated. Thanks.

Related

Importing CSV into MySQL with White Space

Anyone have any success importing a csv file into MySQL?
Here's an example csv file:
Full name,Source - Name
John,Youtube
Jon,FB
Jacob,Twitter
Here's the code:
CREATE TABLE Person
(ID INT AUTO_INCREMENT primary key,
fullname CHAR not null,
sourcenam1 CHAR,
);
LOAD DATA LOCAL INFILE '/Users/...DDB.csv' INTO TABLE t1
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
(Full name, Source - Name)
But I get an error that says my syntax is wrong. Specifically, it is the following:
ERROR 1064 (42000): You have an error in your SQL syntax
I would like to not have to delete the column names to solve this problem.
The names in the LOAD DATA column list should be the column names in the table, not the headings in the CSV file.
You also need to use the IGNORE option to skip over the heading line.
LOAD DATA LOCAL INFILE '/Users/...DDB.csv' INTO TABLE t1
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
IGNORE 1 LINES
(fullname, sourcenam1)

with mysql load data infile I get "incorrect integer value"

LOAD DATA INFILE 'thefile.csv'
INTO TABLE word
FIELDS TERMINATED BY ';'
IGNORE 1 LINES
SET id = NULL;
I am a bit lost of what to do here, I thought this query would work but it doesn't. I get the error about id, which is why I thought set id = NULL would make it good but no. The point of my id row (which is AUTO_INCREMENT) is that I don't need to specify the ID. so.. ehm. Any help would be greatly appreciated
Incorrect integer value: '' for column 'id' at row 1
My CSV file content:
id;meaning;japanese;kanji;kana;romaji;visible;featured;image;image_author;imgauthor_link;extra
;pants;パンツ;;パンツ;pantsu;;;;;;
You must specify all column names except the id column:
LOAD DATA INFILE 'thefile.csv'
INTO TABLE word
FIELDS TERMINATED BY ';'
IGNORE 1 LINES
(col1,col2,col3);
I assumed your table has (id,col1,col2,col3) columns.
You can use a two step load:
1) Load the data into a table;
2) Use INSERT INTO ... SELECT ... FROM to get your data in your table
Example:
CREATE TABLE baseData (
All your column definitions, but not your id column
);
INSERT INTO (all your columns except the id column) SELECT * FROM baseData;
Alternative option:
Create your table with the id as last column, then the loading with LOAD DATA INFILE works

Insert column from *.txt file to end of existing MySQL table

I am trying to load the contents of a *.txt file to the end of a MySQL database table.i.e. I want to append the contents of the file to an existing table in MySQL. I am working in MySQL Workbench (Windows 7). I downloaded the Windows 5.6.21 installer from http://dev.mysql.com/downloads/windows/installer/:
Here is the MySQL code that I am working with:
Create database, table and load file:
CREATE SCHEMA fileimport2;
USE fileimport2;
CREATE TABLE testtable(column1 INT, column2 VARCHAR(255), column3 DECIMAL(8,4));
LOAD DATA INFILE 'File.txt'
INTO TABLE foo COLUMNS TERMINATED BY '\t';
At this point, the database has one table with 3 rows and 3 columns. NOTE: I have tried:
SET GLOBAL local_infile = 'ON';
but I still cannot get
LOAD DATA LOCAL INFILE 'File2.txt'
to work. For this reason, I am instead using:
LOAD DATA INFILE 'File2.txt'
Next, I add a 4th column at the end of the table:
ALTER TABLE testtable
ADD column4 VARCHAR(255)
AFTER column3;
At this point, the 4th column is created.
Now, I want try to load the file named 'File2.txt' into rows 1,2,3 of column # 4:
Put values into column # 4, from a *.txt file that has one column:
LOAD DATA INFILE 'File2.txt'
INTO TABLE testtable
FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'
(#col1,#col2,#col3,#col4) set column4=#col4;
View table:
SELECT * FROM testtable
Problem:
When I do this, the contents of the file 'File2.txt' are added to rows 4,5,6 of column # 4. Rows 1,2,3 of column 4 are NULL. Rows 4,5,6 of columns 1,2,3 are NULL. This is not what I wanted. I do not want the null entries. My final table should be 3 rows X 4 columns.
Image showing problem:
File Contents:
File.txt (the columns are separated by tabs):
1 First line of text 7.24
2 Second line goes here 5.7
3 Sample text on line 3 11.6
File2.txt:
before today
one tow
ssf three
Question:
Is there a way to load the file (like a horizontal concatenation) into rows 1,2,3 of column #4?

Load Data Infile Error with date

I am trying to load a .csv file that has 5 columns into a table that has the same corresponding columns plus a PK. Dates are data type DATE and all others are Varchar(), except PK.
Here is my load data Import:
LOAD DATA INFILE 'C:\Events_Upload.csv'
INTO TABLE db.events
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
Ignore 1 lines
(
Event_Name,
Event_Handle,
Create_Date,
Retire_Date,
Event_Desc
)
The error is :
Error Code: 1292. Incorrect date value: '' for column 'Retire_Date' at row 1
Row one in CSV looks like:
Time to Send Survey,SurvESend,2013-04-10,,Time for the system to send out the Esurveys
How can I make this field NULL and not blank for uploading from CSV?
There is nothing as null in CSV file. What you can do is may be provide some default value for the missing column and replace it with null by the query after insertion into the table.
Or you can have a look at skipping-empty-csv-objects
check this Export null to .csv and https://superuser.com/questions/390031/how-to-write-null-into-csv-from-excel-for-blank-fields

Mysql Load Data for existing column of a table

Initially I have uploaded Using load Data Infile row is having like 100000 Im Using Ubuntu
Example:data
ToneCode....Artist...MovieName...Language
1....................Mj..........Null........... English
3....................AB..........Null........... English
4....................CD.........Null........... English
5....................EF..........Null........... English
But Now I have To update Column MovieName Starting From ToneCode 1 till 100000 row I’m having data in .csv file to update .
Please suggest how to upload the .Csv file for existing table with data
I think the fastest way to do this, using purely MySQL and no extra scripting, would be as follows:
CREATE a temporary table, two columns ToneCode and MovieName same as in your target table
load the data from your new CSV file into that using LOAD DATA INFILE
UPDATE your target table using the INNER JOIN-like syntax that http://dev.mysql.com/doc/refman/5.1/en/update.html describes:
UPDATE items,month SET items.price=month.price WHERE items.id=month.id;
this would “join” the two tables items and month (by using just the “comma-syntax” for an INNER JOIN) using the id column as the join criterion, and update the items.price column with the value of the month.price column.
I Have found a solution as u Guys mentioned above
Soln: example
create table A(Id int primary Key, Name Varchar(20),Artist Varchar(20),MovieName Varchar(20));
Add all my 100000 row using
Load data infile '/Path/file.csv' into table tablename(A) fields terminated by ',' enclosed by'"'
lines terminated by '\n'
(Id,Name,Artist) here movie value is null
create temporary table TA(Id int primary Key,MovieName Varchar(20));
Uploaded data to temporary table TA
Load data infile '/Path/file.csv' into table tablename(A) fields terminated by ',' enclosed by'"'
lines terminated by '\n'(IDx,MovieName)
Now using join as u said
Update Tablename(TA),TableName(A) set A.MovieName=TA.MovieName Where A.Id=TA.Id