473,605 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,,,49 8.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

Jan 10 '06 #1
3 2189
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,,,49 8.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
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,,,49 8.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
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
7500
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 key and want that to fail? I know that if I try a plain insert this will work, but in the case where I am trying to insert a duplicate key, the insert fails (as it should) and an error is logged. I could first do a check to see if there is...
0
1256
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. Strangely enough, at least one character is working o.k. Could someone enlighten my why....? (using SuSE v9.0)
16
3864
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 table, or perhaps bytes, being updated where the engine just decides, screw it, i'll just make a new one. surfed this group and google, but couldn't find anything. the context: we have some java folk who like to parametize/
19
2094
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 addresses larger words only? Shouldn't sizeof be defined to return the smallest number of 'storage units' required to store an object of the type of its operand? As a general point, is there a guide to what aspects of C would fail if run on a...
3
4321
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 3 b 1 4 c 1 5 d
4
2054
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, the new nodes are appended to the original file. What I really want is for the log file to grow as "report nodes" are added. Also, I am a bit concerned about performance, particularly as the file grows in size. I must write to an xml file (vs. a...
7
2323
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. Is that so?
3
22534
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: ----- System.Data.SqlClient.SqlException è stata individuata Message="Bulk Insert: Unexpected end-of-file (EOF) encountered in data file.\r\nOLE DB provider 'STREAM' reported an error. The provider did not give any information about the error.\r\nOLE...
19
2415
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
8009
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7938
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8298
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5892
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5452
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3914
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2441
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1279
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.