Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to INSERT in SQLite, Error code:19

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.

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

 "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);";
like image 716
narancs Avatar asked Aug 05 '10 14:08

narancs


2 Answers

constraint failed

Sounds like your primary key already exists in the table

like image 159
Pentium10 Avatar answered Oct 31 '22 19:10

Pentium10


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.

     "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.

like image 36
Opy Avatar answered Oct 31 '22 20:10

Opy