Ad
Why Is The Inserted Data Going To -1 Row Id?
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
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad