Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:07:38+00:00 2026-05-23T19:07:38+00:00

I’m trying to insert some values on a data base, but it doesn’t work

  • 0

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);
        }

        @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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:07:39+00:00Added an answer on May 23, 2026 at 7:07 pm

    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:

    1. 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);";
    
    1. 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!!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.