约束失败 - SQLite 3 - Android

发布于 2024-10-31 20:23:56 字数 2074 浏览 0 评论 0原文

我的 SQLite 上有下表。

    StringBuilder createSql = new StringBuilder();
    createSql.append("create table LIBRARY ( ");
    createSql.append("id integer primary key, ");
    createSql.append("title text, ");
    createSql.append("author text, ");
    createSql.append("publisher text, ");
    createSql.append("thumbnailUrl text, ");
    createSql.append("formatType text, ");
    createSql.append("contentUrl text, ");
    createSql.append("publicationType text, ");
    createSql.append("favorite text, ");
    createSql.append("started text, ");
    createSql.append("normalizedTitle text, ");
    createSql.append("downloaded integer, ");
    createSql.append("wasDownloaded text ");
    createSql.append(");");

下面的代码在该表上插入一个值。

public void addLibrary(LibraryItem item) {
    ContentValues row = new ContentValues();
    row.put("title", item.getTitle());
    row.put("author", item.getAuthor());
    row.put("publisher", item.getPublisher());
    row.put("thumbnailUrl", item.getThumbnail());
    row.put("formatType", item.getFormat());
    row.put("contentUrl", item.getContentUrl());
    row.put("publicationType", item.getPublicationType());
    row.put("favorite", item.isFavorite() ? "YES" : "NO");
    row.put("id", item.getItemId());
    row.put("normalizedTitle", StringUtil.normalize(item.getTitle()));
    row.put("downloaded", Calendar.getInstance().getTimeInMillis());
    row.put("wasdownloaded", item.hasContent() ? "YES" : "NO");

    long id = db.insert("LIBRARY", null, row);
    add(id, item.getCategories());
}

但是当执行它时,我收到以下错误。

Error inserting id=255 formatType=null author=null title=A Cabana contentUrl=/sdcard/Digital Editions/A Cabana.pdf publicationType=Livro thumbnailUrl=https://api-dls.homolog.abrildigital.com.br/images/22/android_cover_201103092041.jpg wasdownloaded=NO downloaded=1302631109426 normalizedTitle=A Cabana favorite=NO publisher=null
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

怎么了?

I has the following table on my SQLite.

    StringBuilder createSql = new StringBuilder();
    createSql.append("create table LIBRARY ( ");
    createSql.append("id integer primary key, ");
    createSql.append("title text, ");
    createSql.append("author text, ");
    createSql.append("publisher text, ");
    createSql.append("thumbnailUrl text, ");
    createSql.append("formatType text, ");
    createSql.append("contentUrl text, ");
    createSql.append("publicationType text, ");
    createSql.append("favorite text, ");
    createSql.append("started text, ");
    createSql.append("normalizedTitle text, ");
    createSql.append("downloaded integer, ");
    createSql.append("wasDownloaded text ");
    createSql.append(");");

And the following code to inser a value on that Table.

public void addLibrary(LibraryItem item) {
    ContentValues row = new ContentValues();
    row.put("title", item.getTitle());
    row.put("author", item.getAuthor());
    row.put("publisher", item.getPublisher());
    row.put("thumbnailUrl", item.getThumbnail());
    row.put("formatType", item.getFormat());
    row.put("contentUrl", item.getContentUrl());
    row.put("publicationType", item.getPublicationType());
    row.put("favorite", item.isFavorite() ? "YES" : "NO");
    row.put("id", item.getItemId());
    row.put("normalizedTitle", StringUtil.normalize(item.getTitle()));
    row.put("downloaded", Calendar.getInstance().getTimeInMillis());
    row.put("wasdownloaded", item.hasContent() ? "YES" : "NO");

    long id = db.insert("LIBRARY", null, row);
    add(id, item.getCategories());
}

But when executing it, I got the following error.

Error inserting id=255 formatType=null author=null title=A Cabana contentUrl=/sdcard/Digital Editions/A Cabana.pdf publicationType=Livro thumbnailUrl=https://api-dls.homolog.abrildigital.com.br/images/22/android_cover_201103092041.jpg wasdownloaded=NO downloaded=1302631109426 normalizedTitle=A Cabana favorite=NO publisher=null
android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

What's wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫腻 2024-11-07 20:23:56

您的主键不会自动递增。然后您插入第一行,它有一个键,然后您插入另一行,它有相同的键,但主键不能相同!

然后您正在插入具有指定键的项目,我认为该错误意味着具有该键的项目已经存在。

Your primary key isn't auto incrementing. Then you are inserting first row it is got a key, and then you are inserting another one it is got the same one but the primary key can't be same!

Then you are inserting an item with specified key i think the error means what item with this key is already exists.

旧情别恋 2024-11-07 20:23:56

将您的 createSQL 构建器更改为...

createSql.append("id integer primary key autoincrement, ");

另外。当您进行插入时,您没有设置 id 字段。相反,您应该做的是在函数的顶部进行检查。

if(item.getItemID() == 0)
{
    //Run update here instead.
    //Set the id on the ContentValues here
}
else
{
    //Run insert statement instead.
    //Don't set the id in the ContentValues here.
}

Change your createSQL builder to...

createSql.append("id integer primary key autoincrement, ");

Also. When you're doing your insert, you don't set the id field. Instead, what you should do, is do a check at the top of the function.

if(item.getItemID() == 0)
{
    //Run update here instead.
    //Set the id on the ContentValues here
}
else
{
    //Run insert statement instead.
    //Don't set the id in the ContentValues here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文