Why does this insert fail?
Why does this insert fail?
am 11.01.2006 00:15:41 von 4partee
I'm trying to import a csv file with a PHP procedure. However, some of the
lines in the csv file have missing values.
When this command is given to mysql:
insert tablex values
('2006-01-06','IHS',127,,,,,,,3.45,-0.33,12.82,,,,,,40.64,17 .41,79.22,,,,,,3,3,3.00,15,0.51,0.40,0.38,3904,,,498.24,,,,, ,71.03,,,,,,7.098505e-03,,,,,,2.87,,,,,,0.34,,,,,,1.01,1.07, ,
)
Here is the error message:
You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right syntax
to use near
',,,,,3.45,-0.33,12.82,,,,,,40.64,17.41,79.22,,,,,,3,3,3.00, 15,0.51,0.40,0.38,390'
at line 1
The table consists of 69 columns. col 1 is a date, col 2 is char, col
is integer and the remaining 66 are float.
Here is a snip of the table defs:
+-----------+----------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+------------+-------+
| Date | date | | PRI | 0000-00-00 | |
| Symbol | char(10) | | PRI | - | |
| RIL | int(11) | YES | | NULL | |
| Mn26wRet | float | YES | | NULL | |
| SD26wRet | float | YES | | NULL | |
|
Am I going to have to recode this by exploding the csv file line by line
and creating a series of update statements?
John
Re: Why does this insert fail?
am 11.01.2006 07:40:42 von Jonathan
4partee wrote:
> I'm trying to import a csv file with a PHP procedure. However, some of the
> lines in the csv file have missing values.
>
> When this command is given to mysql:
>
> insert tablex values
> ('2006-01-06','IHS',127,,,,,,,3.45,-0.33,12.82,,,,,,40.64,17 .41,79.22,,,,,,3,3,3.00,15,0.51,0.40,0.38,3904,,,498.24,,,,, ,71.03,,,,,,7.098505e-03,,,,,,2.87,,,,,,0.34,,,,,,1.01,1.07, ,
> )
>
> Here is the error message:
>
> You have an error in your SQL syntax; check the
> manual that corresponds to your MySQL server version for the right syntax
> to use near
> ',,,,,3.45,-0.33,12.82,,,,,,40.64,17.41,79.22,,,,,,3,3,3.00, 15,0.51,0.40,0.38,390'
> at line 1
>
> The table consists of 69 columns. col 1 is a date, col 2 is char, col
> is integer and the remaining 66 are float.
>
> Here is a snip of the table defs:
>
> +-----------+----------+------+-----+------------+-------+
> | Field | Type | Null | Key | Default | Extra |
> +-----------+----------+------+-----+------------+-------+
> | Date | date | | PRI | 0000-00-00 | |
> | Symbol | char(10) | | PRI | - | |
> | RIL | int(11) | YES | | NULL | |
> | Mn26wRet | float | YES | | NULL | |
> | SD26wRet | float | YES | | NULL | |
> |
>
> Am I going to have to recode this by exploding the csv file line by line
> and creating a series of update statements?
>
> John
>
>
>
First of all I think the query should read:
INSERT INTO tablex VALUES
instead of
INSERT tablex VALUES
but this might be a typo. ;-)
If I counted correctly it seems to fail on the first empty field in your
query. I think it needs either a value of 0 or an empty quoted string to
accept an empty field.
As you have 69 fields in your table do you also insert 69 fields in your
query? I did not bother to count, did you?
I believe if you use the syntax you are using that you have to supply a
value for all fields. As they are floats they accept only 0 (zero)
values for empty fields.
Maybe you could try and use a text editor to replace the ',' value by
',0' in your file and then run it again against the database.
However if you do it like below you only have to specify a value for the
columns you specify, the rest will get its default values:
INERT INTO table (field1, field 2, ..., fieldn) VALUES (value1, value2,
...., valuen);
Good luck!
Jonathan
Re: Why does this insert fail?
am 11.01.2006 08:40:25 von gordonb.to04b
>I'm trying to import a csv file with a PHP procedure. However, some of the
>lines in the csv file have missing values.
Nobody said you can stuff a line of CSV into the middle of a SQL
statement and have it work.
>When this command is given to mysql:
>
>insert tablex values
Shouldn't that be: insert into tablex values
>('2006-01-06','IHS',127,,,,,,,3.45,-0.33,12.82,,,,,,40.64,1 7.41,79.22,,,,,,3,3,3.00,15,0.51,0.40,0.38,3904,,,498.24,,,, ,,71.03,,,,,,7.098505e-03,,,,,,2.87,,,,,,0.34,,,,,,1.01,1.07 ,,
>)
>
>Here is the error message:
>
>You have an error in your SQL syntax; check the
>manual that corresponds to your MySQL server version for the right syntax
>to use near
>',,,,,3.45,-0.33,12.82,,,,,,40.64,17.41,79.22,,,,,,3,3,3.00 ,15,0.51,0.40,0.38,390'
>at line 1
>
>The table consists of 69 columns. col 1 is a date, col 2 is char, col
>is integer and the remaining 66 are float.
>
>Here is a snip of the table defs:
>
>+-----------+----------+------+-----+------------+-------+
>| Field | Type | Null | Key | Default | Extra |
>+-----------+----------+------+-----+------------+-------+
>| Date | date | | PRI | 0000-00-00 | |
>| Symbol | char(10) | | PRI | - | |
>| RIL | int(11) | YES | | NULL | |
>| Mn26wRet | float | YES | | NULL | |
>| SD26wRet | float | YES | | NULL | |
>|
>
>Am I going to have to recode this by exploding the csv file line by line
>and creating a series of update statements?
An update statement, or a series of them, will not create a new
record in the table. You need to put something in for the missing
values appropriate for the field. Common fillers include null, '',
0, 3.141592, 'UNKNOWN', or -1.
Gordon L. Burditt
Re: Why does this insert fail?
am 11.01.2006 15:32:46 von 4partee
Thank you, Jonathan;
I got the csv into the table a different way. I used a text editor
to replace all the missing values with \N as they must be null. Here is
the sql:
load data infile 'xyz.csv' into table table1 fields terminated by ','
optionally enclosed by '"' ignore 1 lines
John