如何在不指定主键值的情况下将行插入 SQLite table

How is it possible to insert row into SQLite table without specifying value for the primary key

我正在做一个项目,不理解我在网上找到的这部分代码。 (我也看过其他例子,它们做的完全一样,但我不太明白为什么)

当他们向 table 中插入内容时,他们没有主键值。有人可以向我解释为什么会这样吗?

这是我发现的 2 个代码示例,它们可以执行我上面所述的操作。 谢谢

// As you can see a contact has 3 attributes.
int _id;
String _name;
String _phone_number;


// Where they create a table. As you can see the primary key is ID
@Override
public void onCreate(SQLiteDatabase db)
{
   String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
    + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
       
    db.execSQL(CREATE_CONTACTS_TABLE);
}
       

// Adding new contact
// This is what I don't understand. Why don't they get an ID for the contact. 
// They only have values for the name and phone number when they insert it into the table.
public void addContact(Contact contact) 
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

这是另一个示例,但这是使用一本书。 一本书有 3 个属性,一个 id(主键),一个作者和书名。再一次,他们没有得到主键的值。

public void addBook(Book book)
{
    Log.d("addBook", book.toString());
    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, book.getTitle()); // get title 
    values.put(KEY_AUTHOR, book.getAuthor()); // get author

    // 3. insert
    db.insert(TABLE_BOOKS, // table
        null, //nullColumnHack
        values); // key/value -> keys = column names/ values = column values

    // 4. close
    db.close(); 
}

因为主键是 Autoincrement 因为它是 ROWID 的别名。

来自文档:

In SQLite, table rows normally have a 64-bit signed integer ROWID which is unique among all rows in the same table. (WITHOUT ROWID tables are the exception.)

You can access the ROWID of an SQLite table using one the special column names ROWID, ROWID, or OID. Except if you declare an ordinary table column to use one of those special names, then the use of that name will refer to the declared column not to the internal ROWID.

If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID. You can then access the ROWID using any of four different names, the original three names described above or the name given to the INTEGER PRIMARY KEY column. All these names are aliases for one another and work equally well in any context.

When a new row is inserted into an SQLite table, the ROWID can either be specified as part of the INSERT statement or it can be assigned automatically by the database engine. To specify a ROWID manually, just include it in the list of values to be inserted. For example:

所以在您给出的示例中,id 是由数据库引擎分配的。对于大多数用例,这已经足够了。

您可以创建table喜欢

static final String DATABASE_CREATE = "create table "+TABLE_NAME+"( ID integer primary key autoincrement,user_name  text,user_phone  text,user_email text); ";

然后自动递增

看到这个linkhttp://www.freakyjolly.com/android-sqlite-integration/ http://www.freakyjolly.com/android-sqlite-how-to-insert-rows-in-database/