Ad

Why Is The Inserted Data Going To -1 Row Id?

- 1 answer

When I click on insert data i.e R.id.action_insert_data. When it is clicked insertData method is called. It should shows the row Id of the entry Why is the toast message showing "row id = -1"?

Why new rows are not being created?

So my question is what changes should I make to so it will return row id 1 .

CatalogActivity.java

private nameDBHelper in;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_catalog);

    // Setup FAB to open EditorActivity
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
            startActivity(intent);
        }
    });
    in = new nameDBHelper(this);
}

public void insertData(){

 //   Contract.nameDBHelper in = new Contract.nameDBHelper(this);
    SQLiteDatabase sqLiteDatabase = in.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(Contract.entry.COLUMN_F_NAME, "first");
    contentValues.put(Contract.entry.COLUMN_L_NAME, "last");
    long s = sqLiteDatabase.insert(Contract.entry.TABLE_NAME,null,contentValues);

    Toast.makeText(getApplicationContext(),"row id= " +s,Toast.LENGTH_LONG).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu options from the res/menu/menu_catalog.xml file.
    // This adds menu items to the app bar.
    getMenuInflater().inflate(R.menu.menu_catalog, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // User clicked on a menu option in the app bar overflow menu
    switch (item.getItemId()) {
        // Respond to a click on the "Insert dummy data" menu option
        case R.id.action_insert_data:
            insertData();

            return true;
        // Respond to a click on the "Delete all entries" menu option
        case R.id.action_delete_all_entries:
            // Do nothing for now
            return true;
    }
    return super.onOptionsItemSelected(item);
}

nameDBHelper.java

private static final String DATABASE_NAME = "first";
private static final int VERSION = 1;
public nameDBHelper(Context context) {
    super(context, DATABASE_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    /* String sqlTable = CREATE TABLE table1(_id INTEGER, firstname TEXT NO NULL, second TEXT NO NULL);*/
    String sqlTable = "CREATE TABLE " + Contract.entry.TABLE_NAME + "( " +
            Contract.entry._ID + " INTEGER, " +
            Contract.entry.COLUMN_F_NAME + " TEXT, " +
            Contract.entry.COLUMN_L_NAME + " TEXT);";
    sqLiteDatabase.execSQL(sqlTable);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
Ad

Answer

works after adding "INTEGER PRIMARY KEY AUTOINCREMENT".

Ad
source: stackoverflow.com
Ad