4

I am trying to put this into the database. All rows are correct. Each row is also String/Text, except for "Id" which is an auto-incrementing Int value.

I am getting an unexpected error, however, saying Column count doesn't match value count at row 1. What is wrong with the query?

INSERT INTO  `world2_main`.`Messages` (
`Id` ,
`ToId` ,
`FromId` ,
`Subject` ,
`Message` ,
`Read` ,
`Original Sender` ,
`Date`
)
VALUES (
NULL,  '3611',  '156',  'You are so...',  'Cool.',  '0',  '3611'  '1338590308');
questionto42
  • 7,175
  • 4
  • 57
  • 90
Anonymous
  • 553
  • 5
  • 17
  • 41

4 Answers4

10

well Id is an autoincrementing int value, and you put a null in it.

Just do

INSERT INTO  `world2_main`.`Messages` (
`ToId` ,
`FromId` ,
`Subject` ,
`Message` ,
`Read` ,
`Original Sender` ,
`Date`
)
VALUES (  '3611',  '156',  'You are so...',  'Cool.',  '0',  '3611'  '1338590308');

EDIT :in fact was just a missing comma after 3611. But avoiding inserting id is still good.

INSERT INTO  `world2_main`.`Messages` (
    `ToId` ,
    `FromId` ,
    `Subject` ,
    `Message` ,
    `Read` ,
    `Original Sender` ,
    `Date`
    )
    VALUES (  '3611',  '156',  'You are so...',  'Cool.',  '0',  '3611',  '1338590308');
questionto42
  • 7,175
  • 4
  • 57
  • 90
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • Still errors with "Column count doesn't match value count at row 1" – Anonymous Jun 01 '12 at 22:48
  • Thanks for seeing that. Small mistake that took me days to even come close to, lol. I just saw the error a few minutes ago. Appreciate it mate. – Anonymous Jun 01 '12 at 22:54
6

I have also discovered that if you have a trigger on the table you want to insert into and that trigger have another insert statement with un-matching columns and values, it will throw that error "Column count doesn't match value count at row ".

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
2

You may have defined different number of parameters and are probably passing a different number of parameters.

You may have:

INSERT INTO `buyers`(`key1`,  `key2` )
VALUES (value1,value2,value3 );

or more number of arguments in INSERT INTO than in the VALUES

Vedha Peri
  • 1,386
  • 2
  • 12
  • 11
2

Keep in mind 3 things:

  1. Number of parameters must match
  2. auto increment should be taken care of
  3. (This was my issue) When inserting multiple attributes

don't do this--

insert into agent(eid, ename, email, phone, score) values(
    (2, 'b', 'b', 5, 3),
    (1, 'a', 'a', 5, 3)
   );

You have to do this instead

insert into agent(eid, ename, email, phone, score) values
    -> (1, 'a', 'a', 5, 3),
    -> (2, 'b', 'b', 5, 3);

Thanks

Ojasv singh
  • 474
  • 8
  • 19