jose
jose

Reputation: 83

How to Insert foreignKey Value in SQLite in Android?

I have a table tableGrupo (Groups) that has a foreign key from other table tableMaquinas (Machines). When I am inserting data in the table tableGrupo (Groups), Android Studio tells me that the foreign key field must not be null because it is a foreign key.

What value should I attribute in order to have the insertion accepted and pass it the proper foreign key id?

This is my code:

 //Table1
        db.execSQL("CREATE TABLE "+ tableGrupo +" ("+
                idGrupo +" INTEGER PRIMARY KEY AUTOINCREMENT, "+
                nomeGrupo +" TEXT)");

        //Table2
        db.execSQL("CREATE TABLE "+ tableMaquinas +" ("+
                idMaquina +" INTEGER PRIMARY KEY AUTOINCREMENT, "
                + nomeMaquina + " text not null, "
                + imagemMaquina + " BLOB, "
                + id_grupo + " integer, "
                + " FOREIGN KEY ("+id_grupo+") REFERENCES "+tableGrupo+"("+idGrupo+"))");


**This is the code where I make the insertion in the database**

 public void addMachine(String name, byte[] image) throws SQLiteException {
        ContentValues values = new  ContentValues();
        values.put(nomeMaquina,name);
        values.put(imagemMaquina,image);
        //here i have to put a value for the foreignkey
        this.getWritableDatabase().insert(tableMaquinas,null,values);
    }

Upvotes: 1

Views: 3810

Answers (2)

Girum A
Girum A

Reputation: 73

I know this is an old question, but i think i have a reasonable workaround. It is possible to save to return value from the insert method and save it in a variable. If the insert is successful, it will return the _id when successful, and -1 when fail. Here is a reference of the Documentation.

ContentValues values = new  ContentValues();
values.put(idGrupo, 1);
long sucess = this.getWritableDatabase().insert(tableGrupo, null, values);
values.clear();

values.put(nomeMaquina, name);
values.put(imagemMaquina, image);
values.put(id_grupo, success);
this.getWritableDatabase().insert(tableMaquinas, null, values); 

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

Your table tableMaquinas defines a column id_group as an integer which has a foreign key pointing to a primary key column in the tableGrupo table. So, beyond the requirement is that it has to point to a record in the tableGrupo table which actually exists.

Here is code which should work with your current schema:

ContentValues values = new  ContentValues();
values.put(idGrupo, 1);
this.getWritableDatabase().insert(tableGrupo, null, values);

ContentValues values = new  ContentValues();
values.put(nomeMaquina, name);
values.put(imagemMaquina, image);
values.put(id_grupo, 1);
this.getWritableDatabase().insert(tableMaquinas, null, values);

Note that I have assumed that certain variables above exist (most already do), but you might have to slightly alter the above code to get it to work in your particular method.

Upvotes: 2

Related Questions