0

I'm trying to run the activity "Administration1" that functionality to insert information in database. I used as exemple (cin=admin ; motdepasse=admin ; nometprenom=admin) . But i don't know why it give me this error:

06-17 17:45:03.384 3207-3207/com.example.pc.myapplication E/SQLiteDatabase: Error inserting cin=admin motdepasse=admin nometprenom=admin
android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: contacts.name (code 1299)
    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
    at com.example.pc.myapplication.DatabaseHelper.insertContact(DatabaseHelper.java:52)
    at com.example.pc.myapplication.Administration1.onOKClick(Administration1.java:64)
    at java.lang.reflect.Method.invoke(Native Method)

There are my Databasehelper :

package com.example.pc.myapplication;

     import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
     import android.database.DatabaseErrorHandler;
     import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
     import android.provider.ContactsContract;

      public class DatabaseHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION =1 ;
private static final String DATABASE_NAME = "contacts.db" ;
private static final String TABlE_NAME = "contacts" ;
private static final String TABlE_NAME1 = "Argent" ;
private static final String COLUMN_ID = "id" ;
private static final String COLUMN_NOMETPRENOM = "nometprenom" ;
private static final String COLUMN_CIN = "cin" ;
private static final String COLUMN_MOTDEPASSE = "motdepasse" ;
SQLiteDatabase db;


private static final String TABlE_CREATE = "create table contacts ( id integer primary key not null , nometprenom Text not null , cin Text not null , motdepasse Text not null);" ;

public DatabaseHelper(Context context)
{
    super(context ,DATABASE_NAME , null , DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABlE_CREATE);
     this.db=db;
}

public void insertContact(Contact c)
{
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from contacts" ;
    Cursor cursor = db.rawQuery(query , null) ;
    int count = cursor.getCount();

    values.put(COLUMN_NOMETPRENOM, c.getnometprenom());
    values.put(COLUMN_MOTDEPASSE , c.getmotdepasse());
    values.put(COLUMN_CIN , c.getcin());

    db.insert(TABlE_NAME,null, values);
}



public String searchPass (String nometprenom) {
    db = this.getReadableDatabase();
    String query = "select nometprenom , motdepasse from " +TABlE_NAME;
    Cursor cursor = db.rawQuery(query,null);
    String a,b;
    b= "not found";
    if (cursor.moveToFirst())
    {
        do {
            a = cursor.getString(0);

            if (a.equals(nometprenom)) {
                b = cursor.getString(1);
                break;
            }
        }
        while (cursor.moveToNext());
            }
            return b;
        }


            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int 
         newVersion) {
            String query = "DROP TABLE IF EXISTS "+TABlE_NAME ;
            db.execSQL(query);
            this.onCreate(db);

        }


        }

There is my class Administration1.java :

package com.example.pc.myapplication;

 import android.app.Activity;
  import android.content.Intent;
 import android.os.Bundle;
  import android.widget.EditText;
   import android.widget.Toast;
  import android.view.View;

 public class Administration1 extends Activity {

DatabaseHelper helper = new DatabaseHelper (this);

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.administration1);
   }

     public void onOKClick ( View v )
     {

   if (v.getId() == R.id.buttonokadmin) {

    EditText nometprenom = (EditText)findViewById(R.id.TFnometprenom);
EditText cin = (EditText)findViewById(R.id.TFcin);
EditText pass1 = (EditText)findViewById(R.id.TFpass1);
EditText pass2 = (EditText)findViewById(R.id.TFpass2);

String strnometprenom = nometprenom.getText().toString();
String strcin = cin.getText().toString();
String strpass1 = pass1.getText().toString();
String strpass2 = pass2.getText().toString();

if (strnometprenom.equals(""))
{
    Toast.makeText(this,"tapez nom et prenom ",Toast.LENGTH_SHORT).show();
}
if (strcin.equals(""))
{
    Toast.makeText(this,"tapez CIN ",Toast.LENGTH_SHORT).show();
}
if (strpass1.equals(""))
{
    Toast.makeText(this,"tapez mot de passe ",Toast.LENGTH_SHORT).show();
}
if (strpass2.equals(""))
{
    Toast.makeText(this,"tapez confirmer mot de passe ",Toast.LENGTH_SHORT).show();
}

 if (!strpass1.equals(strpass2)){
    //popup msg
    Toast pass = Toast.makeText(Administration1.this , "Mots de passe fausses!" , Toast.LENGTH_SHORT);
            pass.show();
       }
       else {

    // insert the details in database
    Contact c = new Contact();
    c.setnometprenom(strnometprenom);
    c.setcin(strcin);
    c.setmotdepasse(strpass1);

    helper.insertContact(c);
    Toast pass = Toast.makeText(Administration1.this , "OK" , 
     Toast.LENGTH_SHORT);
    pass.show();
    {
        Intent i;

        i = new Intent(Administration1.this, MainActivity.class);
        startActivity(i);
    }

   }
    }
   }
    }

Thanks in advance for help.

jalel
  • 57
  • 3
  • It sounds like one of the values you put in `values.put(..., ...)` is null in your `insertContact` function. Have you tried checking the values before calling `values.put`? Also, looks like you never assign the key field, which could also be the issue (`values.put(COLUMN_ID, count);`) – Tyler V Jun 17 '18 at 18:21
  • @TylerV the key field (being defined as id INTEGER PRIMARY KEY) is a special case in SQLite. it is saying that the id column is an alias of the rowid, an automatically generated unique integer identifier (unless a unique integer is specified/assigned). Not specfifying a value uses the automatically generated id and is common practice. You may find [SQLite Autoincrement](https://sqlite.org/autoinc.html) of interest. – MikeT Jun 17 '18 at 20:45
  • Delete the App's data, or uninstall the App or increase DBVERSION to 2 and rerun the App. The issue is that the table still has a column named **name**. Although you have deleted the column from the create SQL, the SQL will not be run as the `onCreate` method only runs once. Doing one of the above and rerunning the App will result in the `onCreate` method running. Note exisiting data will be lost. – MikeT Jun 18 '18 at 20:08

0 Answers0