热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

无法在SQLite中插入,错误代码:19-UnabletoINSERTinSQLite,Errorcode:19

WhenImtryingtorunthefollowing:当我试图运行以下内容时:ContentValuescvnewContentValues();cv

When I'm trying to run the following :

当我试图运行以下内容时:

  ContentValues cv = new ContentValues();
  cv.put(table_LocalSettings_Unit, input);
  mDb.insert(table_LocalSettings, "", cv);

I got the following error:

我有以下错误:

Error inserting unit = 0;
SqliteConstraintException: error code 19 constraint failed.

插入单元错误= 0;SqliteConstraintException:错误代码19约束失败。

What should be the problem ? The table sql Code is:

问题应该是什么?表sql代码为:

 "create table if not exists " + table_LocalSettings + "( " + table_LocalSettings_ID
     + " INTEGER PRIMARY KEY NOT NULL , " + table_LocalSettings_MapType
     + " INTEGER NOT NULL , " + table_LocalSettings_Visib + " BIT NOT NULL , "
     + table_LocalSettings_Unit + " INTEGER DEFAULT 0 NOT NULL , "
     + table_LocalSettings_SpeedUnit + " INTEGER NOT NULL , "
     + table_LocalSettings_Alert + " BIT NOT NULL ," + table_LocalSettings_UserID
     + " INTEGER DEFAULT -1 , " + table_LocalSettings_Username + " VARCHAR , "
     + table_LocalSettings_PowerSave + " VARCHAR , " + table_LocalSettings_PremiumUser
     + " INTEGER NOT NULL DEFAULT 0);";

4 个解决方案

#1


11  

constraint failed

约束失败

Sounds like your primary key already exists in the table

听起来您的主键已经存在于表中

#2


4  

I had the same problem and Pentium 10's answer led me in the correct direction. After verifying the bellow code was wrong then correcting it I cleared data from that app in the emulator and it recreated the DB and it works fine now.

我有同样的问题,奔腾10的回答让我找到了正确的方向。在验证了下面的代码是错误的之后,我在模拟器中清除了来自那个应用的数据,它重新创建了DB,现在它可以正常工作了。

     "create table if not exists " + DATABASE_TABLE + " (" + _ID + " integer primary key autoincrement," +
     " ItemName text not null, ItemID text not null, Image text not null, ImageDate text not null);";

I think one of my problems was I had an extra column. I removed one of the columns earlier and did not remove it from the above lines.

我认为我的一个问题是我有一个额外的专栏。我删除了前面的一个列,并没有从上面的行中删除它。

The main thing is tripple check the code for errors and spelling and if using the emulator clear the data.

主要的事情是tripple检查代码中的错误和拼写,如果使用模拟器清除数据的话。

#3


2  

You can see a list of all the SQLite error codes here http://www.sqlite.org/c3ref/c_abort.html. Error code 19 means a table constraint (NOT NULL, UNIQUE, etc.) was violated during the operation (INSERT, etc.). From looking at your table creation logic, many of your fields are set to NOT NULL, yet you are only attempting to insert a single column. If you create your table such that values cannot be null, you have to include a non-null value during the INSERT, otherwise you will see the error code 19. You could also remove the constraint from your database table if it isn't needed.

您可以在这里看到所有SQLite错误代码的列表http://www.sqlite.org/c3ref/c_abort.html。Error code 19表示在操作期间违反了表约束(非NULL, UNIQUE等)(INSERT等)。通过查看表创建逻辑,您的许多字段被设置为NOT NULL,但您只是试图插入一个列。如果创建的表的值不能为空,则必须在插入期间包含一个非空值,否则将看到错误代码19。如果不需要,也可以从数据库表中删除约束。

As a side note, there are other insert methods that allow for handling the constraint violation such as

另外,还有一些插入方法可以处理约束冲突,比如。

db.insertWithOnConflict(..., ..., ..., INTEGER);

where INTEGER is one of the static conflict resolution variables in the SQLiteDatabase class (but I don't think any of them allow for NOT NULL violation). You can also read more about SQLite conflict resolution options here: http://www.sqlite.org/conflict.html

在SQLiteDatabase类中,INTEGER是静态冲突解决变量之一(但我认为其中任何一个都不允许非空冲突)。您也可以在这里阅读关于SQLite冲突解决选项的更多信息:http://www.sqlite.org/conflict.html

#4


2  

You can remove the not null from table and it will work fine. like this:
right:

您可以从表中删除not null,它会正常工作。这样的:正确的:

String callTable = "CREATE TABLE IF NOT EXISTS '%s'" + "(userid VARCHAR, callwith VARCHAR, calltype VARCHAR, callstart time, callend time, callmedia VARCHAR" + ");"

wrong:

错误的:

String callTable = "CREATE TABLE IF NOT EXISTS '%s'" + "(userid VARCHAR not Null , callwith VARCHAR not null, calltype VARCHAR, callstart time, callend time, callmedia VARCHAR" + ");"

推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文探讨了 Objective-C 中的一些重要语法特性,包括 goto 语句、块(block)的使用、访问修饰符以及属性管理等。通过实例代码和详细解释,帮助开发者更好地理解和应用这些特性。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 本文详细介绍了Java中的访问器(getter)和修改器(setter),探讨了它们在保护数据完整性、增强代码可维护性方面的重要作用。通过具体示例,展示了如何正确使用这些方法来控制类属性的访问和更新。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
author-avatar
hhha老窝_349
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有