Error while creating tables simultaneously in sqlite database

Asked

Viewed 209 times

2

I’m developing a mobile application that saves data from a form in the local database, using the sqlite. Only every time the database is reset or changed its version it returns me an error that could not create the tables. These tables have a relationship One To Many.

It is curious that if I create a table first, commenting on the row that creates the second table, and then uncomment the same one it generates both normally. How to do not have this error, and in the forums I saw, this is the way to create the tables with sqlite.

Creating the tables:

public Conexao(Context context) {
    super(context, name, null, version);
}


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table paciente(_id integer primary key autoincrement, nome varchar(50), cpf varchar(11), data varchar(15) )");
    db.execSQL("create table imagens(_id integer primary key autoincrement, status varchar(10), imagem varchar(300), FOREIGN KEY(paciente_id) REFERENCES paciente(_id))");
    Log.i("TAG", "Banco criado com sucesso");

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("drop table paciente");
    db.execSQL("drop table imagens");
    onCreate(db);
}

Inserting a new register:

public long inserirFoto(Foto imagem){

    List<String> fotos = imagem.getImagem();
    ContentValues values = new ContentValues();
    for(int i = 0; i < fotos.size(); i++) {

        values.put("status", "skdoasm");
        values.put("imagem", "skfmsakf");

    }return banco.insert("imagens", null, values);
}

public long inserirPaciente(Paciente paciente){

    ContentValues values = new ContentValues();
    values.put("nome", paciente.getNome());
    values.put("cpf", paciente.getCpf());
    values.put("data", paciente.getData());
    return banco.insert("paciente", null, values);
}

Relationship tables:

public class Paciente implements Serializable {

private int id;
private String nome;
private String cpf;
public  Foto foto;
private String data;
getter e setter...

public class Foto  implements Serializable {

@Expose
private List<String> imagem;
@Expose
private int id;
@Expose
private String status;

public List<String> getImagem() {
    return imagem;
}

getter e setter...

Exit error:

android.database.sqlite.SQLiteException: unknown column "paciente_id" in foreign key definition (code 1): , while compiling: create table imagens(_id integer primary key autoincrement, status varchar(10), imagem varchar(300), FOREIGN KEY(paciente_id) REFERENCES paciente(_id))

1 answer

3


Friend, your second table is with wrong script, see:

create table imagens(
  _id integer primary key autoincrement, 
  status varchar(10), 
  imagem varchar(300), 
  FOREIGN KEY(paciente_id) REFERENCES paciente(_id)
)

You say that paciente_id is your foreign key with reference in the table pacientes spine _id. But there isn’t any paciente_id in the declaration of its table.
Change to:

create table imagens(
  _id integer primary key autoincrement, 
  paciente_id integer NOT NULL,
  status varchar(10), 
  imagem varchar(300), 
  FOREIGN KEY(paciente_id) REFERENCES paciente(_id)
)

Thus remaining:

db.execSQL("create table imagens(_id integer primary key autoincrement, paciente_id integer NOT NULL, status varchar(10), imagem varchar(300), FOREIGN KEY(paciente_id) REFERENCES paciente(_id))");
  • I’ve made the changes now you’re returning to me android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: imagens.paciente_id (code 1299)

  • @Technologyget this error probably should be showing up when you do a right Insert? Take a look at your Insert because it should not be informed the value for the patient field_id!

  • in my case, that the patient has a relationship with photo and photo has a string arraylist(path of these photos) in SQL looks like this: image varchar(300)

  • @Technologiadanet only one note, if this table can store null values for the paciente_id field, then change the creat command I passed and remove NOT NULL from pacient_id, then create the table again to resolve this error

  • @Tecnologiadanet exactly because patient has relationship with table pictures you need to insert patient id into it as I’m telling you, if not how can you show which image is of which patient? Post gave him Insert that I help him

  • worked out, Murilo, thanks

Show 2 more comments

Browser other questions tagged

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