473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2178
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jean-Christian Imbeault | last post by:
I have a table with a primary field and a few other fields. What is the fastest way to do an insert into that table assuming that sometimes I might try to insert a record with a duplicate primary...
0
by: Frank van Vugt | last post by:
Hi, I noticed that queries from one of my php-clients were failing when they contained certain accented characters, but upon trying the same thing in psql, it showed failure there as well....
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
19
by: James Harris | last post by:
My K&R 2nd ed has in the Reference Manual appendix, A7.4.8 sizeof yields the number of BYTES required to store an object of the type of its operand. What happens if C is running on a machine that...
3
by: Hai Nguyen | last post by:
Hi all I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field (PR) (PR) ID Project Ans 1 2 a 1 ...
4
by: SteveW | last post by:
Thanks to some good help from a previous post, I have been able to create well formed xml as part of a report logger app. However, I still have a small problem. When I add new xml to the log file,...
7
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. ...
3
by: AB | last post by:
Hi to all, I have a problem about a importation of a file *.csv with SQL Server, through a bulk insert, called in a store procedure that a c# sw calls. This is the description of the error:...
19
by: active | last post by:
The ColorPalette class has no constructor so how does one use it? I define a variable by: Dim cp as ColorPalette but don't know how assign an object to the variable. Thanks in advance
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.