-1

i am writing my first application, and I am also trying to incorporate a database, why sqlitedatabase insert method return -1 ??? i'm confused...

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "MyDatabase";
private static final String TABLE_CONTACT = "contact";
private static final String id_col = "id";
private static final String name_col = "name";
private static final String ph_num_col = "phone_number";

public static final String CREATE__TABLE_contact=
        "CREATE TABLE " + TABLE_CONTACT + " (" +
                id_col + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                name_col + " TEXT, " +
                ph_num_col + " TEXT" + ")";
public SqliteDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    //sakhte table haye morede niaz....
    sqLiteDatabase.execSQL(CREATE__TABLE_contact);
}

..............................................................

public void addContact(Contact contact){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name",contact.getName());
    values.put("phone_number",contact.getPhone_number());

    Long test = db.insert(TABLE_CONTACT, null, values);
    if(test==  -1) {
        Log.i("insert", ""+test);
    }
    db.close();

}
Hamid
  • 23
  • 1
  • 5

1 Answers1

0

Please make sure that you defined your content provider subclass in AndroidManifest.xml file. Please find the following sample code. (You have to change according to your application.)

<provider
    android:name="com.example.app.MyDataBase"
    android:authorities="com.example.app"
    android:exported="false">
</provider>

Please refer android developer page, link.

Thomas07
  • 82
  • 1
  • 9