Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: SQLiteConstraintException: error code 19: constraint failed

I checked other examples in SO and I searched a lot, nothing is working for me. The database file is this (after suggested edit).

Error

E/Database(274): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
E/Database(274):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
E/Database(274):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
E/Database(274):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
E/Database(274):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
E/Database(274):    at com.example.nycgasstationhunter.userRegister$2.onClick(userRegister.java:51)
E/Database(274):    at android.view.View.performClick(View.java:2408)
E/Database(274):    at android.view.View$PerformClick.run(View.java:8816)
E/Database(274):    at android.os.Handler.handleCallback(Handler.java:587)
E/Database(274):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/Database(274):    at android.os.Looper.loop(Looper.java:123)
E/Database(274):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/Database(274):    at java.lang.reflect.Method.invokeNative(Native Method)
E/Database(274):    at java.lang.reflect.Method.invoke(Method.java:521)
E/Database(274):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/Database(274):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/Database(274):    at dalvik.system.NativeStart.main(Native Method)

Code

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;

public class userRegister extends Activity{
//database
SQLiteDatabase db;
DBHelper dbhelper;
Context ourContext;
ContentValues cv;

 public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_register);

//database  
 dbhelper=new DBHelper(this);
 db=dbhelper.getWritableDatabase();
 cv=new ContentValues();
//editText
final EditText userEdit=(EditText)  findViewById(R.id.userEdit);
final EditText emailEdit=(EditText) findViewById(R.id.emailEdit);
final EditText passwordEdit=(EditText)  findViewById(R.id.passwordEdit);
final EditText retypePassEdit=(EditText) findViewById(R.id.retypePassEdit);
//Register button
    Button regButton = (Button) findViewById(R.id.regButton);
      regButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
    //string
    final String userName=userEdit.getText().toString();
    final String emailAddress=emailEdit.getText().toString();
    final String password=passwordEdit.getText().toString();
    final String retypePassword=retypePassEdit.getText().toString();

         if (userName.length()!=0){
            if (emailAddress.length()!=0){
              if (password.length()!=0){
                 if (retypePassword.equals(password)){
                   //save in DB
                    cv.put(DBHelper.USER, userName);
                    cv.put(DBHelper.EMAIL,emailAddress);
                    cv.put(DBHelper.PASSWORD, retypePassword); 

                    db.insert(DBHelper.USER_TABLE, null, cv);

                    Intent intent = new Intent (userRegister.this,Profile.class);    
                     startActivity(intent);
               }
                 else 
                     Toast.makeText(userRegister.this,"Password mismatch", Toast.LENGTH_SHORT).show(); 
            }
              else 
                     Toast.makeText(userRegister.this,"Invalid password", Toast.LENGTH_SHORT).show();
        }
            else 
                 Toast.makeText(userRegister.this,"Invalid email", Toast.LENGTH_SHORT).show();
    }
         else 
             Toast.makeText(userRegister.this,"Invalid user name", Toast.LENGTH_SHORT).show();
 }
});
}
}

When I insert information and press regButton, it sends me to Profile activity. There is no information being saved in database. I don't understand the error in LogCat and how can I solve that? Why there is nothing in database? (I am using SQLite Database Browser to check data.) Thank you

like image 998
Evana Hill Avatar asked Feb 15 '23 06:02

Evana Hill


2 Answers

The error SQLiteConstraintException indicates that an integrity constraint was violated.

From your Create Table SQL command I think the first problem is your primary key. It should be declare as autoincrement

Change String createDB:

public final String createDB="create table "+USER_TABLE+"("
                    + C_ID + " integer primary key autoincrement, "
                    + USER + " text not null,"  
                    + EMAIL + " text not null," 
                    + PASSWORD + " text not null," 
                    + TIME + " text not null);";  

Also, all fields are declared not null, you should test if retypePassword is not null and you must set a value to field TIME.

EDIT

In order the autoincrement take effect, the database version value need to be incremented.
In DBHelper class change DATABASE_VERSION=1; to DATABASE_VERSION=2;

like image 159
ramaral Avatar answered Feb 16 '23 20:02

ramaral


If the database schema is as follows as you mention in comments (from this question)...

public final String createDB="create table "+USER_TABLE+"("
                     +C_ID+" integer primary key, "
                     +USER+" text not null,"  
                     +EMAIL+ " text not null," 
                     +PASSWORD+ " text not null," 
                     +TIME+ " text not null);";

... then you have not specified a value for the TIME column which has the not null constraint.

Some options:

  • Add a TIME value to the ContentValues before insert().

  • Change the schema and provide a suitable default such as

    TIME + " text not null default current_timestamp"
    

    As always, when changing the database schema, remove the old database so that onCreate() gets called with the new code (clear app data or just uninstall the app).

like image 35
laalto Avatar answered Feb 16 '23 21:02

laalto