472,145 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

Why does this insert fail?

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.09 8505e-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

Jan 10 '06 #1
3 2071
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.09 8505e-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
Jan 11 '06 #2
>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,17.41,79.22,,,,,,3,3,3.00,15 ,0.51,0.40,0.38,3904,,,498.24,,,,,,71.03,,,,,,7.09 8505e-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
Jan 11 '06 #3

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
Jan 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Jean-Christian Imbeault | last post: by
16 posts views Thread by robert | last post: by
3 posts views Thread by Hai Nguyen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.