I get an error in the AUTOINCREMENT-AndroidStudio field

1

I want to create a database for a calendar and I need to create a field of type AUTOINCREMENT .

The fact is that when I create the database in Android Studio with SQLite it underlines me and gives me the following error:

  

'(', ')', or comma expected, got 'AUTOINCREMENT'.

The code is as follows:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;

public class BDSQLite extends SQLiteOpenHelper {
private String sql = "create table eventos(" +
        "idEvento int AUTOINCREMENT," +
        "nombreEvento varchar(40)," +
        "ubicacion varchar(60)," +
        "fechadesde date," +
        "horadesde time," +
        "fechahasta date," +
        "horahasta time," +
        "descripcion varchar(60))";

public BDSQLite(Context context, String name, SQLiteDatabase.CursorFactory 
factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(sql);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

Thank you for your time.

    
asked by Hello There 19.12.2018 в 10:41
source

3 answers

2

AUTOINCREMENT can only be used with PRIMARY KEY , and must be INTEGER

idEvento INTEGER PRIMARY KEY  AUTOINCREMENT

Official Documentation

    
answered by 19.12.2018 / 13:49
source
0

Review the features of AUTOINCREMENT ,

  
  • The AUTOINCREMENT keyword enforces an overload of CPU, memory, disk space, and disk I / O overload and must   be avoided if it is not strictly necessary.
  •   
  • Usually not necessary. In SQLite, a column with the type INTEGER PRIMARY KEY is an alias for ROWID (except in the tables   WITHOUT ROWID) which is always a 64-bit signed integer.
  •   
  • In an INSERT, if the ROWID or INTEGER PRIMARY KEY column does not receive an explicit value, it will be filled automatically with an integer no   used, usually one more than the largest ROWID currently in   use. This is true regardless of whether or not the   keyword AUTOINCREMENT.
  •   
  • If the AUTOINCREMENT keyword appears after INTEGER PRIMARY KEY, that changes the automatic ROWID allocation algorithm for   avoid the reuse of ROWID during the lifespan of the base   data. In other words, the purpose of AUTOINCREMENT is to avoid   ROWID reuse of previously deleted rows.
  •   

In fact, the error message usually appears:

  

Uncaught Error: AUTOINCREMENT is only allowed on an INTEGER PRIMARY   KEY

Therefore it is necessary to define as PRIMARY KEY the field that you will define as AUTOINCREMENT .

idEvento integer PRIMARY KEY AUTOINCREMENT

This would be the query that would create the table correctly:

private String sql = "create table eventos(" +
        "idEvento integer PRIMARY KEY AUTOINCREMENT," +
        "nombreEvento varchar(40)," +
        "ubicacion varchar(60)," +
        "fechadesde date," +
        "horadesde time," +
        "fechahasta date," +
        "horahasta time," +
        "descripcion varchar(60))";
    
answered by 19.12.2018 в 17:51
0

According to the sqlLite documentation:

"Because the keyword AUTOINCREMENT changes the behavior of the ROWID selection algorithm, UTOINCREMENT is not allowed in tables WITHOUT ROWID or in any column of the table other than INTEGER PRIMARY KEY . attempt to use AUTOINCREMENT in a table WITHOUT ROWID or in a column other than the column INTEGER PRIMARY KEY generate an error. "

You will have to add:

  idEvento INTEGER PRIMARY KEY AUTOINCREMENT
    
answered by 19.12.2018 в 13:58