Problem with SQLliteConstraintException: error code 19
I'm trying to insert some values on a data base, but it doesn't work (the app gives me an SQLliteConstraintException 19: constraint failed). I've read a lot about this (in most of cases is caused by dupicate keys, but I think that is not my case). Here's id my DBHelper code:
public class DBHelper {
public static final String DB_NAME = "proyecto";
public static final String DB_TABLE = "usuarios";
public static final int DB_VERSION = 3;
private static final String CLASSNAME = DBHelper.class.getSimpleName();
private static final String[] COLS = new String[] { "_id", "login", "password", "nombre", "apellidos", "n_voluntario" };
private static final String LOGTAG = null;
private SQLiteDatabase db;
private final DBOpenHelper dbOpenHelper;
//
// inner classes
//
public static class Usuario {
public long id;
public String login;
public String password;
public String nombre;
public String apellidos;
public int n_voluntario;
public Usuario() {
}
public Usuario(final long id, final String login, final String password, final String nombre,
final String apellidos, final int n_voluntario) {
this.id = id;
this.login = login;
this.password = password;
this.nombre = nombre;
this.apellidos = apellidos;
this.n_voluntario = n_voluntario;
}
/*@Override
public String toString() {
return this.zip + " " + this.city + ", " + this.region;
}*/
}
private static class DBOpenHelper extends SQLiteOpenHelper {
private static final String DB_CREATE = "CREATE TABLE "
+ DBHelper.DB_TABLE
+ " (_id INTEGER PRIMARY KEY, login TEXT, password TEXT, nombre TEXT, apellidos TEXT, n_voluntario INTEGER);";
public DBOpenHelper(final Context context) {
super(context, DBHelper.DB_NAME, null, DBHelper.DB_VERSION);
}
开发者_Go百科 @Override
public void onCreate(final SQLiteDatabase db) {
try {
db.execSQL(DBOpenHelper.DB_CREATE);
} catch (SQLException e) {
Log.e(LOGTAG, DBHelper.CLASSNAME, e);
}
}
@Override
public void onOpen(final SQLiteDatabase db) {
super.onOpen(db);
}
@Override
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DBHelper.DB_TABLE);
onCreate(db);
}
}
//
// end inner classes
//
public DBHelper(final Context context) {
this.dbOpenHelper = new DBOpenHelper(context);
establishDb();
}
private void establishDb() {
if (this.db == null) {
this.db = this.dbOpenHelper.getWritableDatabase();
}
}
public void cleanup() {
if (this.db != null) {
this.db.close();
this.db = null;
}
}
public long insert(final Usuario usuario) {
ContentValues values = new ContentValues();
values.put("login", usuario.login);
values.put("password", usuario.password);
values.put("nombre", usuario.nombre);
values.put("apellidos", usuario.apellidos);
values.put("n_voluntario", usuario.n_voluntario);
return this.db.insert(DBHelper.DB_TABLE, null, values);
}
public void update(final Usuario usuario) {
ContentValues values = new ContentValues();
values.put("login", usuario.login);
values.put("password", usuario.password);
values.put("nombre", usuario.nombre);
values.put("apellidos", usuario.apellidos);
values.put("n_voluntario", usuario.n_voluntario);
this.db.update(DBHelper.DB_TABLE, values, "_id=" + usuario.id, null);
}
public void delete(final long id) {
this.db.delete(DBHelper.DB_TABLE, "_id=" + id, null);
}
public void delete(final String login) {
this.db.delete(DBHelper.DB_TABLE, "login='" + login + "'", null);
}
public Usuario get(final String login) {
Cursor c = null;
Usuario usuario = null;
try {
c = this.db.query(true, DBHelper.DB_TABLE, DBHelper.COLS, "login = '" + login + "'", null, null, null, null,
null);
if (c.getCount() > 0) {
c.moveToFirst();
usuario = new Usuario();
usuario.id = c.getLong(0);
usuario.login = c.getString(1);
usuario.password = c.getString(2);
usuario.nombre = c.getString(3);
usuario.apellidos = c.getString(4);
usuario.n_voluntario = c.getInt(5);
}
} catch (SQLException e) {
Log.v(LOGTAG, DBHelper.CLASSNAME, e);
} finally {
if (c != null && !c.isClosed()) {
c.close();
}
}
return usuario;
}
}
Any idea?
Thanks for answers.
SQLliteConstraintException exception is raised when an integrity constraint was violated. You have declared "_id" field in "usuarios" Table as a Primary key. By doing this you are putting a contraint on the "_id" column. However when you are inserting you are not supplying value of "_id". There are 2 solution:
- Easy way
change
private static final String DB_CREATE = "CREATE TABLE " +
DBHelper.DB_TABLE + " (_id INTEGER PRIMARY KEY, login TEXT, password
TEXT, nombre TEXT, apellidos TEXT, n_voluntario
INTEGER);";
to
private static final String DB_CREATE = "CREATE TABLE "+ DBHelper.DB_TABLE
+ " (_id INTEGER PRIMARY KEY AUTOINCREMENT, login TEXT, password TEXT,
nombre TEXT, apellidos TEXT, n_voluntario INTEGER);";
- While you insert the value, Also insert "_id" and its corresponding value make sure you put an unique value for "_id" else it will be rejected.
Hope it solves your problem!! If not, Please dump the stack Trace here!!
Cheers!!
In Android's implementation of SQLite, is an autoincrementing key created when the PK is defined as INTEGER PRIMARY KEY? Or do you need to add AUTOINCREMENT as part of the PK definition?
精彩评论