Android 为什么插入的数据将变为-1行Id?

Android 为什么插入的数据将变为-1行Id?,android,Android,当我点击插入数据,即R.id.action\u插入数据时。 单击时,将调用insertData方法。它应该显示条目的行Id 为什么toast消息显示“row id=-1” 为什么不创建新行 所以我的问题是我应该做什么更改,这样它将返回行id 1 CatalogActivity.java private nameDBHelper in; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreat

当我点击插入数据,即R.id.action\u插入数据时。 单击时,将调用insertData方法。它应该显示条目的行Id 为什么toast消息显示“row id=-1”

为什么不创建新行

所以我的问题是我应该做什么更改,这样它将返回行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) {

}

我在CREATETABLE查询中做了一些更改

String sqlTable = "CREATE TABLE " + Contract.entry.TABLE_NAME + "( " +
        Contract.entry._ID + " INTEGER AUTOINCREMENT, " +
        Contract.entry.COLUMN_F_NAME + " TEXT, " +
        Contract.entry.COLUMN_L_NAME + " TEXT);";

使用此选项,每次在表中输入条目时,您的ID都将自动递增。希望这有帮助。

因为您的
\u id
列没有自动递增功能。您必须手动为
\u id
列提供值

将自动增量约束添加到
\u id
列。像这样

String sqlTable = "CREATE TABLE " + Contract.entry.TABLE_NAME + "( " +
        Contract.entry._ID + " INTEGER AUTOINCREMENT, " +
        Contract.entry.COLUMN_F_NAME + " TEXT, " +
        Contract.entry.COLUMN_L_NAME + " TEXT);";

添加“INTEGER主键自动增量”后工作。

我对代码做了以下更改,现在可以工作了。我添加了“主键自动增量”

String sqlTable = "CREATE TABLE " + Contract.entry.TABLE_NAME + "( " + Contract.entry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Contract.entry.COLUMN_F_NAME + " TEXT, " + Contract.entry.COLUMN_L_NAME + " TEXT);";