热门标签 | 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" + ");"

推荐阅读
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社区 版权所有