Android   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – SQLite PRIMARY键AutoIncrement不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试使用自动递增主键的表.包括用于创建表的SQL查询.
问题是自动增量不起作用.当我插入一个NULL为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时,它返回正确的值,但实际的数据库字段为null.

如果我理解正确声明INTEGER PRIMARY KEY的列将自动增量. [Cite]

解决方法:

来自文档:

您不必在具有PRIMary KEY约束的columN上添加UNIQUE约束.

说明:

而是添加NOT NULl.
这就是为什么:

我建议使用此列定义:

create table conversations (
    conversation_id INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT,
...
}

大佬总结

以上是大佬教程为你收集整理的android – SQLite PRIMARY键AutoIncrement不起作用全部内容,希望文章能够帮你解决android – SQLite PRIMARY键AutoIncrement不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: