Bom dia,
Estou tentando salvar dados de um programa em Java para o SQLite. Achei que por eu ter colocado a chave primária como autoincrement que o BD faria esse incremento sozinho, mas não está funcionando.
Seguem os códigos:
Java:
public boolean saveUserData(Usuario u){ //obj chega ok
try{
stmt = con.prepareStatement("INSERT INTO Usuario (nome, sobrenome, "
+ "usuario, dtNascimento, telefone) VALUES (?,?,?,?,?)");
stmt.setString(1, u.getNome());
stmt.setString(2, u.getSobrenome());
stmt.setString(3, u.getUsuario());
//TODO melhorar data (essa e PainelCad)
java.util.Date d = u.getDtNascimento();
stmt.setDate(4, new java.sql.Date(d.getTime()));
stmt.setString(5, u.getTelefone());
stmt.executeUpdate();
return true;
} catch (SQLException e) {
System.err.println("Erro ao inserir usuário ou conta\n");
e.printStackTrace();
return false;
}
}
SQL:
create table CONTA(
id_conta INTEGER NOT NULL PRIMARY KEY
--GENERATED ALWAYS AS IDENTITY //Tentei isso e não funcionou, vi num fórum
-- (START WITH 1, INCREMENT BY 1),
email TEXT,
senha TEXT
)
create table USUARIO(
id_usuario INTEGER PRIMARY KEY AUTOINCREMENT,
id_conta INT NOT NULL,
nome TEXT NOT NULL,
sobrenome TEXT NOT NULL,
usuario TEXT NOT NULL,
dtNascimento DATE,
telefone TEXT,
FOREIGN KEY (id_conta) REFERENCES CONTA (id_conta)
)
Estou recebendo o erro:
org.sqlite.SQLiteException: [SQLITE_CONSTRAINT_NOTNULL] A NOT NULL constraint failed (NOT NULL constraint failed: USUARIO.id_conta)
Podem ajudar?