I am trying to insert data into a mysql table from a csv file. I am using the infile sql command, but I am having trouble because the first column of the table is an id that is set as an auto increment field. what do I have to set my first column value to in order to get this to work, or can I do it at all?
Thanks
Try using an empty field or a TAB character as a first column value in a file
(see comment "Posted by Mohamed Abdulla on August 17 2005 11:14am" on http://dev.mysql.com/doc/refman/5.0/en/loading-tables.html)
Another solution:
LOAD DATA INFILE 'DATA.txt' INTO TABLE your_table
FIELDS TERMINATED BY '^'
OPTIONALLY ENCLOSED BY '~'
(column2, column3, ... )
SET column1 = NULL
Related
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
(MySQL) I have a table with 2 columns.
Table 2 column is a varbinary column.
I have txt file that has 2040 binary strings converted to numbers ( ie 000001 = 000001 , 000100 = 000004 etc).
I am tring to find a statement to insert the data into table. i tried
"LOAD DATA LOCAL INFILE 'C:/ProjectFolder/MySQLHex/Hex.txt' INTO TABLE testbinary
LINES TERMINATED BY '\n'
(#col1,#col2) set representation=#col2; " .
It inserted all rows as NULL values.
If i manually insert 1 row (insert statment), it works !! How do i load the txt file into 2 column of the table using a command ?
You're missing a FIELDS TERMINATED BY '=' in your statement. Therefore the whole line is treated as one column.
It inserted all rows as NULL values, because you load the whole line from your txt file into variables, and just the second column (which is NULL because your whole line is in variable #col1) is set to the variable #col2.
Since you don't do any transformations or whatever with your variables, those are completely unnecessary in this case. Just insert directly into the columns, without using variables.
i use below script for insert data to sql from textpad.
#!/bin/bash
mysql --utest -ptest test << EOF
LOAD DATA INFILE 'test.txt'
INTO TABLE content_delivery_process
FIELDS TERMINATED BY ',';
EOF
in my test file i have a format like,
cast , date , name , buy
i can insert but i need format like below,
S.NO | date | name | buy | cast
You can specify the columns you want to import:
From the MySQL Manual:
MySQL LOAD DATA INFILE
The following example loads all columns of the persondata table:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata;
By default, when no column list is provided at the end of the LOAD
DATA INFILE statement, input lines are expected to contain a field for
each table column.
If you want to load only some of a table's columns, specify a column
list:
LOAD DATA INFILE 'persondata.txt' INTO TABLE persondata (col1,col2,...);
You must also specify a column list if the order of the fields in the
input file differs from the order of the columns in the table.
Otherwise, MySQL cannot tell how to match input fields with table
columns.
You would include "FIELDS TERMINATED BY '|';" at the end to import data delimited with a '|' symbol.
Hope this helps.
create table [YOUR TABLE] ( `S.NO` INT AUTO_INCREMENT, date DATETIME, name VARCHAR(50), buy VARCHAR(50), cast VARCHAR(50));
Load data local infile 'test.txt' ignore into table [YOUR TABLE] fields terminated by ',' lines terminated by '\n'(cast , date , name , buy);
I'm trying to convert timestamps on the fly when importing a csv file into mysql from string to datetime data type. But I am getting a #1411 - Incorrect datetime value: '2007-03-30 16:01:15' for function str_to_date error.
The SQL:
load data infile 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
into table test
fields terminated by ','
lines terminated by '\n'
(date, col1,col2,col3,col4)
SET
date = str_to_date(date,'%Y.%m.%d %H:%i:%s.%f');
All rows in the .csv are formated like this:
2007.03.30 16:01:15.901,117.53,117.55,35600000,43700000
I've applied
SELECT str_to_date(date,'%Y.%m.%d %H:%i:%s.%f') FROM test
to sample data that was already stored in mysql, it did work.
The target row date is set to DATETIME.
You need to go via a user variable. As the manual says:
The column list can contain either column names or user variables. With user variables, the SET clause enables you to perform transformations on their values before assigning the result to columns.
User variables in the SET clause can be used in several ways. The following example uses the first input column directly for the value of t1.column1, and assigns the second input column to a user variable that is subjected to a division operation before being used for the value of t1.column2:
LOAD DATA INFILE 'file.txt'
INTO TABLE t1
(column1, #var1)
SET column2 = #var1/100;
In your case:
LOAD DATA INFILE 'C:/ProgramData/MySQL/MySQL Server 5.5/data/testfile.csv'
INTO TABLE test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(#date, col1, col2, col3, col4)
SET date = STR_TO_DATE(#date, '%Y.%m.%d %H:%i:%s.%f');
Is it possible to use "load data local infile" in mysql and set a column to a certain value...
So for example if my commannd is:
mysql_query('load data local infile "'.$filename.'" into table products fields terminated by "|" lines terminated by "\n" (column2, column3, column4)');
Then have a table like this
Column 1 | Column 2 | Column 3 | Column 4
can set column 1 equal to value I choose and have it done in this query? I am trying to have column 1 be equal to the filename it is importing...how would I set this into the sql query?
According to load data infile syntax you can do it like this:
mysql_query('load data local infile "'.$filename.'" into table products fields terminated by "|" lines terminated by "\n" (column2, column3, column4) SET column1=\'' . $filename . '\'');
From the link above: "The SET clause can be used to supply values not derived from the input file..."
(I'm not sure if something is messed up with single/double quotes...didn't try it).