Error inserting in SQLite database

前端 未结 5 1191
失恋的感觉
失恋的感觉 2021-01-28 11:30

I\'m trying to create the following database:

public static final String KEY_NAME = \"nombre\";
public static final String KEY_ROWID = \"_id\";
public static fin         


        
5条回答
  •  渐次进展
    2021-01-28 11:37

    I haven't worked with Android, so this could be off, but it appears you're sending the ID with the insert into CAPAS table, but it's unnececessary because you have it defined as AUTOINCREMENT.

    You have:

    public long createCapa(String id, String nombre) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_ID, id);
        initialValues.put(KEY_NAME, nombre);
    
        return mDb.insert(DATABASE_TABLE_CAPAS, null, initialValues);
    }
    

    What happens if you do this?

     public long createCapa( String nombre) {
         ContentValues initialValues = new ContentValues();
        //initialValues.put(KEY_ID, id);
        initialValues.put(KEY_NAME, nombre);
    
        return mDb.insert(DATABASE_TABLE_CAPAS, null, initialValues);
      }
    

    and this:

         createCapa("Escuelas");
    

    Is it really common practice to define both _id and id in Android? When you define the pk as integer primary key in SQLite it becomes an alias for the rowid automatically. Here's how I would do it normally:

           create table capas (id integer primary key autoincrement, nombre text not null);
    

提交回复
热议问题