Why does this insert fail?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 4partee

    #1

    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



  • Jonathan

    #2
    Re: Why does this insert fail?

    4partee wrote:[color=blue]
    > 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
    >
    >
    >[/color]
    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


    Comment

    • Gordon Burditt

      #3
      Re: Why does this insert fail?

      >I'm trying to import a csv file with a PHP procedure. However, some of the[color=blue]
      >lines in the csv file have missing values.[/color]

      Nobody said you can stuff a line of CSV into the middle of a SQL
      statement and have it work.
      [color=blue]
      >When this command is given to mysql:
      >
      >insert tablex values[/color]

      Shouldn't that be: insert into tablex values[color=blue]
      >('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?[/color]

      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

      Comment

      • 4partee

        #4
        Re: Why does this insert fail?


        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


        Comment

        Working...