PRIMARY KEY AUTOINCREMENT - Android Studio Error

Asked

Viewed 162 times

0

I’m trying to insert records with auto increment, but every time I get failure return. But if it’s auto increment he shouldn’t ask me for the fifth record?

Database code:

database1.execSQL("CREATE TABLE IF NOT EXISTS ssxx (idEvento INTEGER
PRIMARY KEY  AUTOINCREMENT, data varchar(20),valor
varchar(20),descricao varchar(20),forma varchar(20));");

Functional Inclusion Code:

database1.execSQL("INSERT INTO ssxx VALUES('" + EDNome.getText() +
"','" + EDFone.getText() + "','" + EDesc.getText() + "','" +
radioButton.getText() + "')");

The Error:

  android.database.sqlite.SQLiteException: table ssxx has 5 columns but 4 values were supplied (code 1 SQLITE_ERROR): , while compiling: INSERT INTO ssxx VALUES('05/07/2019','11','q','Vista')
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)

He says I’m trying to enter 4 records, but my database has 5 fields. Auto increment should not automatically fill in id field?

  • 2

    If you want a field to be included with its DEFAULT value then this field cannot appear in the field list. In your case you need to put the list of fields for which you are reporting values. See the documentation.

2 answers

0

  • According to the manual it shall be AUTOINCREMENT.

  • Really, I’ll update!

0


I understand your point of view, but you must specify which values you will be putting in the table in order. You can (and should) put only the 4 values that do not match your id and let the Database take care of the auto-increment as follows:

database1.execSQL("INSERT INTO ssxx(data, valor, descricao, forma) VALUES('" + EDNome.getText() + "','" + EDFone.getText() + "','" + EDesc.getText() + "','" + radioButton.getText() + "')");

If you do not specify exactly the 4 values of the tuple you want to add, the BD will wait for you to pass as parameter the amount of columns that that table has, and if you pass some different amount of that it will not know what to do.

On the other hand, if you specify the 4 values, it will associate face one of the parameters passed with their respective columns, and the one left (the autoincrement) it will take care of the part

I hope I’ve helped!

  • thank you so much as I did not see it!! thank you even gave it right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.