problems with inserting records in Android

4

I have a problem entering usuario and password , by console it throws the following:

  

table users have 3 columns but 2 values were supplied

and in the emulator the app stops, the table was created with two fields and an id auto increment, use android studio, attached code

public void onCreate(SQLiteDatabase db) {
   db.execSQL("create table usuarios(codigo integer primary key autoincrement, usuario text, contrasena text)");
   db.execSQL("insert into usuarios values('admin', 'admin')");
}



@Override
public void onUpgrade(SQLiteDatabase db, int versionVieja, int versionNueva)
 {
  db.execSQL("create table usuarios(codigo integer primary key autoincrement, usuario text, contrasena text)");
   db.execSQL("insert into usuarios values('admin', 'admin')");
 }
    
asked by Claudia Astudillo Perez 13.11.2016 в 17:59
source

3 answers

1

In addition to the other answers you can also make the insert

insert into table values (null, valor_1, valor2)

According to the documentation of sqlite THIS is the form since when an INTEGER PRIMARY KEY field is declared, it is already self-incriminating.

  

(1) How do I create an AUTOINCREMENT field.

     

Short answer: A column declared INTEGER PRIMARY KEY will   autoincrement.

     

If you declare a column of a table to be INTEGER PRIMARY KEY, then   whenever you insert NULL into that column of the table, the NULL is   automatically converted into an integer.

In Spanish:

  

(1) How to create an AUTOINCREMENT field

     

Short answer: When the column is declared as INTEGER PRIMARY   KEY will autoincrement.

     

If a column is declared in the table as INTEGER PRIMARY KEY, each   Once a NULL is inserted within that value, the NULL value   automatically becomes an integer that is the largest of all   values of that column.

    
answered by 17.12.2016 в 13:11
0

The same error message tells you the problem:

  

table users have 3 columns but 2 values were supplied

The users table contains 3 columns but in your query you define only 2:

db.execSQL("insert into usuarios values('admin', 'admin')")

Here the solution is to define the fields you wish to insert

"insert into usuarios(usuario, contrasena) values('admin', 'admin')"

This would be the solution:

public void onCreate(SQLiteDatabase db) {
   db.execSQL("create table usuarios(codigo integer primary key autoincrement, usuario text, contrasena text)");
   db.execSQL("insert into usuarios(usuario, contrasena) values('admin', 'admin')");
}



@Override
public void onUpgrade(SQLiteDatabase db, int versionVieja, int versionNueva)
 {
  db.execSQL("create table usuarios(codigo integer primary key autoincrement, usuario text, contrasena text)");
   db.execSQL("insert into usuarios(usuario, contrasena) values('admin', 'admin')");
 }
    
answered by 15.11.2016 в 20:03
-1

The Insertion syntax seems incorrect considering the AUTOINCREMENT . To fix it, you could specify the fields to be filled in your query

db.execSQL("insert into usuarios(usuario,contrasena) values('admin', 'admin')");

Or passing the value NULL . According to the Documentation

  

If a column in a table is declared to be INTEGER PRIMARY KEY , then every time a NULL value is inserted in the table column, the NULL is automatically converted into a whole number that is one greater than the greater value of that column over all the other rows of the table, or 1 if the table is empty

db.execSQL("insert into usuarios values(null,'admin', 'admin')");
    
answered by 13.11.2016 в 21:24