Made SQL database, but ID is always 0.

How to fix?

public void onCreate(SQLiteDatabase db) { db.execSQL("create table " + TABLE_CONTACTS + "(" + KEY_ID + " integer primary key autoincrement," + KEY_DATE + " text," + KEY_HOUR + " text," + KEY_MINUTE + " text," + KEY_TODO + " text" + ")"); } 

2

 if (id == R.id.btn_back1) { Cursor cursor = database.query(DBHelper.TABLE_CONTACTS, null, null, null, null, null, null); if (cursor.moveToFirst()) { int idIndex = cursor.getColumnIndex(DBHelper.KEY_ID); int todoIndex = cursor.getColumnIndex(DBHelper.KEY_TODO); do { Log.d("my_logs", "ID = " + cursor.getInt(idIndex) + ", todo = " + cursor.getString(todoIndex)); } while (cursor.moveToNext()); } else Log.d("my_logs","0 rows"); cursor.close(); Intent intent = new Intent(AddingActivity.this, CalendarActivity.class); startActivity(intent); } else if (id == R.id.btn_add1) { cv.put(DBHelper.KEY_DATE, CalendarActivity.selectedDate); cv.put(DBHelper.KEY_HOUR, timePicker.getHour()); cv.put(DBHelper.KEY_MINUTE, timePicker.getMinute()); cv.put(DBHelper.KEY_TODO, name); database.insert(DBHelper.TABLE_CONTACTS, null, cv); Toast.makeText(AddingActivity.this, "TODO successfully added", Toast.LENGTH_LONG).show(); Intent intent = new Intent(AddingActivity.this, MainActivity.class); startActivity(intent); } 

Closed due to the fact that off-topic participants Kromster , aleksandr barakin , cheops , user194374, Nicolas Chabanovsky ♦ July 14 '16 at 4:51 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, aleksandr barakin, cheops, Community Spirit, Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • And how do you fill the table with data, how do you select them? - Kromster
  • I take this data from one input field, and the rest of the fields (date and time automatically) are Serhii Stets am
  • Add this to the question, maybe there is a problem, but not in the creation of the table - Kromster
  • Added another part ... - Serhii Stets
  • try autoincrement in general to remove and rebuild the project, removing it from the device before it - Android Android

1 answer 1

And if auto write so write?

 AUTO_INCREMENT 
  • It doesn't work anyway! (( - Serhii Stets
  • It seems that you do not need to specify an auto-increment for the primary key - it is automatically used. Here you can see: stackoverflow.com/questions/16832401/… - msi
  • This is not really the answer - Vladyslav Matviienko