error: "table sales has 6 columns but 5 values were supplied"

1

I created the following table in sql

CREATE TABLE vendas  
    (  
     ID INTEGER IDENTITY(1,1),  
     Empresa VARCHAR (20),  
     Modelo  TEXT,  
     Preco REAL,
     Kilometragem REAL,
     Ano INTEGER,
     PRIMARY KEY(ID)
    );

I put the ID column that has an IDENTITY property as the primary key

By what I researched, when inserting a new tuple into the table I can omit the ID field because it is self-generated, but I did the insertion.

INSERT INTO vendas VALUES ("Movidas","RENAULT SANDERO 1.6 EXPRESSION 8V FLEX 4P MANUAL", 36590.0,25.002,2017)

and returns the error

table vendas has 6 columns but 5 values were supplied: INSERT INTO vendas VALUES ("Movidas","RENAULT SANDERO 1.6 EXPRESSION 8V FLEX 4P MANUAL", 36590.0,25.002,2017)

Can anyone help me solve this problem, please?

Personal, I did as the friend guastallaigor helped me, but now that I did the insertions came another problem in the id field. It's just going to NULL for this field.

    
asked by anonymous 18.05.2018 / 04:41

2 answers

1

Try something like this if the ID is incrementally in the database:

INSERT INTO vendas (Empresa, Modelo, Preco, Kilometragem, Ano) VALUES ("Movidas","RENAULT SANDERO 1.6 EXPRESSION 8V FLEX 4P MANUAL", 36590.0,25.002,2017)

Otherwise, try to place an ID manually to see if it works:

INSERT INTO vendas VALUES (1, "Movidas","RENAULT SANDERO 1.6 EXPRESSION 8V FLEX 4P MANUAL", 36590.0,25.002,2017)

EDIT: In case IDs are null, AUTOINCREMENT is probably missing when creating the vendas table in question.

Assuming by the image that the database is SQLite, it is not possible to perform a ALTER TABLE . So, if you can, the easiest way is to drop the entire table ( DROP TABLE vendas ) and create it again, something like:

CREATE TABLE vendas  
(  
 ID INTEGER IDENTITY(1,1) AUTOINCREMENT,  
 Empresa VARCHAR (20),  
 Modelo  TEXT,  
 Preco REAL,
 Kilometragem REAL,
 Ano INTEGER,
 PRIMARY KEY(ID)
);

Otherwise you should create a new table:

CREATE TABLE vendasaux  
(  
 ID INTEGER IDENTITY(1,1) AUTOINCREMENT,  
 Empresa VARCHAR (20),  
 Modelo  TEXT,  
 Preco REAL,
 Kilometragem REAL,
 Ano INTEGER,
 PRIMARY KEY(ID)
);

Then insert all the records of the vendas table into the vendasaux table:

INSERT INTO vendasaux (Empresa, Modelo, Preco, Kilometragem, Ano) SELECT Empresa, Modelo, Preco, Kilometragem, Ano from vendas;

Then you can now drop the vendas table:

DROP TABLE vendas

Finally, rename the table vendasaux to vendas

ALTER TABLE vendasaux RENAME TO vendas

Note: I will not be able to test them now, but I think it will work,     

18.05.2018 / 04:45
1

When executing the query without informing the name of the columns, the bank can not know which value should be entered in which column. In the code:

INSERT INTO vendas VALUES ("Movidas","RENAULT SANDERO 1.6 EXPRESSION 8V FLEX 4P MANUAL", 36590.0,25.002,2017)

It could try inserting "Moved" into the ID column for example.

You can only omit the name of the columns if you have a value for each in the query.

So you need to enter the name of the columns:

Insert into vendas (Empresa,Modelo,Preco,Kilometragem,Ano) values ("Movidas","RENAULT SANDERO 1.6 EXPRESSION 8V FLEX 4P MANUAL", 36590.0,25.002,2017);
  

Comments:

  • In the Modelo field, you are using Text , which is only recommended for very large texts because it is stored in the blobs area. The ideal would be Varchar . ( See this answer )

  • In the Ano field you are using Integer which can store a very large number (2,147,483,647) which is totally unnecessary. It could only be a NUMERIC(4,0) , at least until year 9999 ... after that let another programmer solve =]

  • On the other fields, I do not know which base you're using, I also do not know the REAL specification, so I will not get into the question.

18.05.2018 / 04:49