约束失败 - SQLite 3 - Android
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的主键不会自动递增。然后您插入第一行,它有一个键,然后您插入另一行,它有相同的键,但主键不能相同!
然后您正在插入具有指定键的项目,我认为该错误意味着具有该键的项目已经存在。
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.
将您的 createSQL 构建器更改为...
另外。当您进行插入时,您没有设置 id 字段。相反,您应该做的是在函数的顶部进行检查。
Change your createSQL builder to...
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.