Home > Software engineering >  SQLite Error: table contacts has 5 columns but 10 values were supplied
SQLite Error: table contacts has 5 columns but 10 values were supplied

Time:12-22

Hi I'm using sqlite on programiz for a bit of coursework to create some tables. I'm trying to then insert some values into the tables but I'm getting this error:

Error: table contacts has 5 columns but 10 values were supplied.

I don't understand why it is viewing the code im inputting as columns instead of rows.

When I remove 5 of the entries, I get a different error:

Error: row value misused

    -- 5 columns
    -- primary key & correct datatypes
    -- show table structures
    -- enter 10 records and view
    -- update a record
    -- delete a record
    -- create relational database (primary key from first table included)
    
      
        CREATE TABLE contacts (
          personal_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
          first_name TEXT NOT NULL,
          last_name TEXT NOT NULL, 
          email_address TEXT NOT NULL UNIQUE, 
          phone_number TEXT NOT NULL UNIQUE
        );
        INSERT INTO contacts 
        VALUES 
  (
    (
      'James', 'May', '[email protected]', 
      '0306 999 0278'
    ), 
    (
      'James', 'Bay', '[email protected]', 
      '0113 496 0664'
    ), 
    (
      'James', 'Cray', '[email protected]', 
      '0116 496 0578'
    ), 
    (
      'James', 'Faye', '[email protected]', 
      '0116 496 0629'
    ), 
    (
      'James', 'Way', '[email protected]', 
      '0151 496 0764')
    ), 
    (
      'James', 'Pay', '[email protected]', 
      '0141 496 04678'
    ), 
    (
      'James', 'Lay', '[email protected]', 
      '0116 496 0840'
    ), 
    (
      'James', 'Whey', '[email protected]', 
      '0113 496 0744'
    ), 
    (
      'James', 'Say', '[email protected]', 
      '0161 496 08588'
    ), 
    (
      'James', 'Gray', '[email protected]', 
      '0114 496 0401'
    )
  );

CodePudding user response:

Your syntax is wrong.

First you must list in the INSERT statement all the names of the columns that will receive the values that you insert (which are all the columns of the table except the primary key since it is autoincremented).

Then remove the parentheses that enclose all the row values:

INSERT INTO contacts (first_name, last_name, email_address, phone_number) VALUES 
    ('James', 'May', '[email protected]', '0306 999 0278'), 
    ('James', 'Bay', '[email protected]', '0113 496 0664'), 
    ('James', 'Cray', '[email protected]', '0116 496 0578'), 
    ('James', 'Faye', '[email protected]', '0116 496 0629'), 
    ('James', 'Way', '[email protected]', '0151 496 0764'), 
    ('James', 'Pay', '[email protected]', '0141 496 04678'), 
    ('James', 'Lay', '[email protected]', '0116 496 0840'), 
    ('James', 'Whey', '[email protected]', '0113 496 0744'), 
    ('James', 'Say', '[email protected]', '0161 496 08588'), 
    ('James', 'Gray', '[email protected]', '0114 496 0401');

See the demo.

  • Related