【发布时间】:2015-08-06 00:23:16
【问题描述】:
我正在尝试创建一个带有自动递增主键的表。包括用于创建表的 SQL 查询。
问题是自动增量不起作用。这意味着当我插入一行以 NULL 作为conversation_id 的值时,它只会插入null。我在多个表上都有这个问题。
-- Table: conversations
CREATE TABLE conversations (
conversation_id INTEGER (64) PRIMARY KEY
UNIQUE,
target_id BIGINT (64),
sender_id BIGINT (64),
status STRING (16) NOT NULL
DEFAULT unseen,
is_group INTEGER (1) NOT NULL
DEFAULT (0),
last_update INTEGER (32) DEFAULT (0),
target_name STRING (64),
target_photo STRING (256),
unread_count INTEGER (10) DEFAULT (0),
last_message STRING (256)
);
以下是我用来插入表格的方法:
public Conversation addConversation(Conversation conversation) {
SQLiteDatabase db = getWritableDatabase();
ContentValues row = new ContentValues();
row.put("target_id", conversation.getTargetID());
row.put("sender_id", conversation.getSenderID());
row.put("target_name", conversation.getTargetName());
row.put("target_photo", conversation.getTargetPhoto());
row.put("status", conversation.getStatus());
row.put("unread_count", conversation.getUnreadCount());
row.put("last_message", conversation.getLastMessage());
conversation.setConversationID(db.insert(TBL_CONVERSATIONS, null, row));
Log.d(TAG, "conversation added: "+conversation.getConversationID());
db.close();
return conversation;
}
这里奇怪的是,当我从 insert 方法中检索插入 id 时,它返回正确的值,但实际的数据库字段为空。
如果我理解正确A column declared INTEGER PRIMARY KEY will autoincrement.[Cite]
【问题讨论】:
-
以下划线开头,例如:_id。
-
@Krupal 没有成功我的朋友。
标签: android sqlite android-sqlite auto-increment