为什么插入的数据转到-1行ID?

时间:2019-04-24 10:08:39

标签: android

当我单击插入数据时,即R.id.action_insert_data。 单击时,将调用insertData方法。它应该显示条目的行ID 为什么吐司消息显示“行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) {

}

4 个答案:

答案 0 :(得分:1)

我在您的创建表查询中做了一些更改。

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都会自动递增。希望这会有所帮助。

答案 1 :(得分:0)

由于您的_id列没有AUTOINCREMENT。您必须手动提供_id列的值。

_id列中添加AUTOINCREMENT约束。这样。

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);";

答案 2 :(得分:0)

在添加“ INTEGER PRIMARY KEY AUTOINCREMENT”之后才能工作。

答案 3 :(得分:0)

我对代码进行了以下更改,现在可以使用了。我添加了“ PRIMARY KEY AUTOINCREMENT”

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);";