MySQL INSERT problem

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

    MySQL INSERT problem

    I am trying to insert data into the fields id and term of the table
    key. id is an auto-incrementing field.

    Each of the following forms fails:-

    INSERT INTO key (id, term) VALUES (0, 'dog')

    INSERT INTO key ('id', 'term') VALUES (0, 'dog')

    INSERT INTO 'key' ('id', 'term') VALUES (0, 'dog')

    INSERT INTO 'key' (id, term) VALUES (0, 'dog')

    mysql_error() returns messages of the following form:-

    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 'key (id,
    term) VALUES (0, 'dog')' at line 1

    What is wrong with the syntax?

    Caversham

  • Malcolm Dew-Jones

    #2
    Re: MySQL INSERT problem

    Caversham (acaversh@yahoo .com) wrote:
    : I am trying to insert data into the fields id and term of the table
    : key. id is an auto-incrementing field.

    : Each of the following forms fails:-

    : INSERT INTO key (id, term) VALUES (0, 'dog')

    : INSERT INTO key ('id', 'term') VALUES (0, 'dog')

    : INSERT INTO 'key' ('id', 'term') VALUES (0, 'dog')

    : INSERT INTO 'key' (id, term) VALUES (0, 'dog')

    : mysql_error() returns messages of the following form:-

    : 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 'key (id,
    : term) VALUES (0, 'dog')' at line 1

    : What is wrong with the syntax?

    Guessing, but is key a keyword? You might need to use a different name
    for the table.


    --

    This space not for rent.

    Comment

    • Nicholas Sherlock

      #3
      Re: MySQL INSERT problem

      Malcolm Dew-Jones wrote:[color=blue]
      > Caversham (acaversh@yahoo .com) wrote:
      > : I am trying to insert data into the fields id and term of the table
      > : key. id is an auto-incrementing field.
      >
      > : Each of the following forms fails:-
      >
      > : INSERT INTO key (id, term) VALUES (0, 'dog')
      >
      > : INSERT INTO key ('id', 'term') VALUES (0, 'dog')
      >
      > : INSERT INTO 'key' ('id', 'term') VALUES (0, 'dog')
      >
      > : INSERT INTO 'key' (id, term) VALUES (0, 'dog')
      >
      > : mysql_error() returns messages of the following form:-
      >
      > : 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 'key (id,
      > : term) VALUES (0, 'dog')' at line 1
      >
      > : What is wrong with the syntax?
      >
      > Guessing, but is key a keyword? You might need to use a different name
      > for the table.[/color]

      If this is so, he should be able to use backticks ' ` ' to do the job:

      INSERT INTO `key` (id, term) VALUES (0, 'dog')

      Cheers,
      Nicholas Sherlock

      Comment

      • Ken Robinson

        #4
        Re: MySQL INSERT problem



        Caversham wrote:[color=blue]
        > I am trying to insert data into the fields id and term of the table
        > key. id is an auto-incrementing field.
        >
        > Each of the following forms fails:-
        >
        > INSERT INTO key (id, term) VALUES (0, 'dog')
        >
        > INSERT INTO key ('id', 'term') VALUES (0, 'dog')
        >
        > INSERT INTO 'key' ('id', 'term') VALUES (0, 'dog')
        >
        > INSERT INTO 'key' (id, term) VALUES (0, 'dog')
        >
        > mysql_error() returns messages of the following form:-
        >
        > 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 'key (id,
        > term) VALUES (0, 'dog')' at line 1[/color]

        Use the following syntax:

        insert into `key` set term='dog';

        If you don't specify the auto increment field, it will "do the right
        thing", autoincrement. Any other fields you don't specify will take
        their default values.

        Ken

        Comment

        • Tom Thackrey

          #5
          Re: MySQL INSERT problem


          On 17-Jun-2005, "Caversham" <acaversh@yahoo .com> wrote:
          [color=blue]
          > INSERT INTO key (id, term) VALUES (0, 'dog')
          >
          > INSERT INTO key ('id', 'term') VALUES (0, 'dog')
          >
          > INSERT INTO 'key' ('id', 'term') VALUES (0, 'dog')
          >
          > INSERT INTO 'key' (id, term) VALUES (0, 'dog')
          >
          > mysql_error() returns messages of the following form:-
          >
          > 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 'key (id,
          > term) VALUES (0, 'dog')' at line 1
          >
          > What is wrong with the syntax?[/color]

          "key" is a mysql keyword and needs to be enclosed in backticks: `key`



          --
          Tom Thackrey

          tom (at) creative (dash) light (dot) com
          do NOT send email to jamesbutler@wil lglen.net (it's reserved for spammers)

          Comment

          Working...