android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed:
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Error
FATAL EXCEPTION: main
Process: com.appmaster.akash.messageplus, PID: 14373
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: UserData.RecieversID (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1579)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:918)
at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:709)
at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:76)
at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:442)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Inserting
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, MessageRecieverId);
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
No problem in inserting but while updating im getting this error
Updating
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();int userData = 0;
Cursor data2 = helper.getUserData();
while (data2.moveToNext()) {
userData = data2.getInt(data2.getColumnIndex("MessagesSent"));
}
ContentValues contentValues2 = new ContentValues();
contentValues2.put(KEY_ID, MessageRecieverId);
contentValues2.put(KEY_MESSAGES_SENT, userData+1);
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
if (returnVariable2 == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(),"uf", Toast.LENGTH_SHORT).show();
}
Anyone know why this is so? .................................................................................................................................................
java android sql sqlite android-studio
add a comment |
Error
FATAL EXCEPTION: main
Process: com.appmaster.akash.messageplus, PID: 14373
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: UserData.RecieversID (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1579)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:918)
at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:709)
at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:76)
at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:442)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Inserting
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, MessageRecieverId);
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
No problem in inserting but while updating im getting this error
Updating
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();int userData = 0;
Cursor data2 = helper.getUserData();
while (data2.moveToNext()) {
userData = data2.getInt(data2.getColumnIndex("MessagesSent"));
}
ContentValues contentValues2 = new ContentValues();
contentValues2.put(KEY_ID, MessageRecieverId);
contentValues2.put(KEY_MESSAGES_SENT, userData+1);
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
if (returnVariable2 == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(),"uf", Toast.LENGTH_SHORT).show();
}
Anyone know why this is so? .................................................................................................................................................
java android sql sqlite android-studio
Because you are violating the database's constraint. The columnUserData.RecieversID
may only hold unique values which you seem to ignore in your code while updating.
– f1sh
Nov 26 '18 at 21:43
add a comment |
Error
FATAL EXCEPTION: main
Process: com.appmaster.akash.messageplus, PID: 14373
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: UserData.RecieversID (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1579)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:918)
at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:709)
at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:76)
at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:442)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Inserting
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, MessageRecieverId);
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
No problem in inserting but while updating im getting this error
Updating
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();int userData = 0;
Cursor data2 = helper.getUserData();
while (data2.moveToNext()) {
userData = data2.getInt(data2.getColumnIndex("MessagesSent"));
}
ContentValues contentValues2 = new ContentValues();
contentValues2.put(KEY_ID, MessageRecieverId);
contentValues2.put(KEY_MESSAGES_SENT, userData+1);
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
if (returnVariable2 == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(),"uf", Toast.LENGTH_SHORT).show();
}
Anyone know why this is so? .................................................................................................................................................
java android sql sqlite android-studio
Error
FATAL EXCEPTION: main
Process: com.appmaster.akash.messageplus, PID: 14373
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: UserData.RecieversID (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForChangedRowCount(Native Method)
at android.database.sqlite.SQLiteConnection.executeForChangedRowCount(SQLiteConnection.java:734)
at android.database.sqlite.SQLiteSession.executeForChangedRowCount(SQLiteSession.java:754)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:64)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1579)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1525)
at com.appmaster.akash.messageplus.Chat.mMessagesSent(Chat.java:918)
at com.appmaster.akash.messageplus.Chat.sendMessage(Chat.java:709)
at com.appmaster.akash.messageplus.Chat.access$900(Chat.java:76)
at com.appmaster.akash.messageplus.Chat$5.onClick(Chat.java:442)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Inserting
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, MessageRecieverId);
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
No problem in inserting but while updating im getting this error
Updating
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();int userData = 0;
Cursor data2 = helper.getUserData();
while (data2.moveToNext()) {
userData = data2.getInt(data2.getColumnIndex("MessagesSent"));
}
ContentValues contentValues2 = new ContentValues();
contentValues2.put(KEY_ID, MessageRecieverId);
contentValues2.put(KEY_MESSAGES_SENT, userData+1);
long returnVariable2 = db.update(TABLE_USER_DATA, contentValues2,null,null);
if (returnVariable2 == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
//-1 means there was an error updating the values
} else {
Toast.makeText(getApplication(),"uf", Toast.LENGTH_SHORT).show();
}
Anyone know why this is so? .................................................................................................................................................
java android sql sqlite android-studio
java android sql sqlite android-studio
asked Nov 26 '18 at 21:35
user10643013
Because you are violating the database's constraint. The columnUserData.RecieversID
may only hold unique values which you seem to ignore in your code while updating.
– f1sh
Nov 26 '18 at 21:43
add a comment |
Because you are violating the database's constraint. The columnUserData.RecieversID
may only hold unique values which you seem to ignore in your code while updating.
– f1sh
Nov 26 '18 at 21:43
Because you are violating the database's constraint. The column
UserData.RecieversID
may only hold unique values which you seem to ignore in your code while updating.– f1sh
Nov 26 '18 at 21:43
Because you are violating the database's constraint. The column
UserData.RecieversID
may only hold unique values which you seem to ignore in your code while updating.– f1sh
Nov 26 '18 at 21:43
add a comment |
1 Answer
1
active
oldest
votes
You are likely inserting using a value for the id column (KEY_ID). Odds on this column is defined as either INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT.
Typically you do not specify a value for such a column as SQLite will assign a unique value (typically 1 greater than the highest assigned).
If you specify a value for such a column and a row in the table already has that value in the column then the UNIQUE constraint implied by PRIMARY KEY will be violated as it's against the rule that a PRIMARY KEY has to be unique.
I'd suggest that you want to use :-
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
returnVariable will then be the value of the id column (KEY_ID) that was assigned when the row was inserted.
- Note id column does not necessarily mean that the column is named id but that it's a special column due to how it is defined. That is, by specifying INTEGER PRIMARY KEY, the column is actually an alias of the normally hidden rowid column (unless the TABLE is defined with the WITHOUT ROWID phrase).
You may find reading SQLite Autoincrement helpful.
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489427%2fandroid-database-sqlite-sqliteconstraintexception-unique-constraint-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are likely inserting using a value for the id column (KEY_ID). Odds on this column is defined as either INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT.
Typically you do not specify a value for such a column as SQLite will assign a unique value (typically 1 greater than the highest assigned).
If you specify a value for such a column and a row in the table already has that value in the column then the UNIQUE constraint implied by PRIMARY KEY will be violated as it's against the rule that a PRIMARY KEY has to be unique.
I'd suggest that you want to use :-
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
returnVariable will then be the value of the id column (KEY_ID) that was assigned when the row was inserted.
- Note id column does not necessarily mean that the column is named id but that it's a special column due to how it is defined. That is, by specifying INTEGER PRIMARY KEY, the column is actually an alias of the normally hidden rowid column (unless the TABLE is defined with the WITHOUT ROWID phrase).
You may find reading SQLite Autoincrement helpful.
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
add a comment |
You are likely inserting using a value for the id column (KEY_ID). Odds on this column is defined as either INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT.
Typically you do not specify a value for such a column as SQLite will assign a unique value (typically 1 greater than the highest assigned).
If you specify a value for such a column and a row in the table already has that value in the column then the UNIQUE constraint implied by PRIMARY KEY will be violated as it's against the rule that a PRIMARY KEY has to be unique.
I'd suggest that you want to use :-
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
returnVariable will then be the value of the id column (KEY_ID) that was assigned when the row was inserted.
- Note id column does not necessarily mean that the column is named id but that it's a special column due to how it is defined. That is, by specifying INTEGER PRIMARY KEY, the column is actually an alias of the normally hidden rowid column (unless the TABLE is defined with the WITHOUT ROWID phrase).
You may find reading SQLite Autoincrement helpful.
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
add a comment |
You are likely inserting using a value for the id column (KEY_ID). Odds on this column is defined as either INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT.
Typically you do not specify a value for such a column as SQLite will assign a unique value (typically 1 greater than the highest assigned).
If you specify a value for such a column and a row in the table already has that value in the column then the UNIQUE constraint implied by PRIMARY KEY will be violated as it's against the rule that a PRIMARY KEY has to be unique.
I'd suggest that you want to use :-
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
returnVariable will then be the value of the id column (KEY_ID) that was assigned when the row was inserted.
- Note id column does not necessarily mean that the column is named id but that it's a special column due to how it is defined. That is, by specifying INTEGER PRIMARY KEY, the column is actually an alias of the normally hidden rowid column (unless the TABLE is defined with the WITHOUT ROWID phrase).
You may find reading SQLite Autoincrement helpful.
You are likely inserting using a value for the id column (KEY_ID). Odds on this column is defined as either INTEGER PRIMARY KEY or INTEGER PRIMARY KEY AUTOINCREMENT.
Typically you do not specify a value for such a column as SQLite will assign a unique value (typically 1 greater than the highest assigned).
If you specify a value for such a column and a row in the table already has that value in the column then the UNIQUE constraint implied by PRIMARY KEY will be violated as it's against the rule that a PRIMARY KEY has to be unique.
I'd suggest that you want to use :-
MainData helper = new MainData(this); //Change the name to your Helper Class name
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
//contentValues.put(KEY_ID, MessageRecieverId); //<<<<<<<<<< COMMENTED OUT (can delete the line)
contentValues.put(KEY_NAME, MessageRecieverName);
contentValues.put(KEY_MESSAGES_SENT, 0);
contentValues.put(KEY_MESSAGES_RECIEVED, 0);
contentValues.put(KEY_TIME_SPENT, "");
long returnVariable = db.insert(TABLE_USER_DATA,null,contentValues);
if (returnVariable == -1) {
Toast.makeText(getApplication(), "Nope", Toast.LENGTH_LONG).show();
returnVariable will then be the value of the id column (KEY_ID) that was assigned when the row was inserted.
- Note id column does not necessarily mean that the column is named id but that it's a special column due to how it is defined. That is, by specifying INTEGER PRIMARY KEY, the column is actually an alias of the normally hidden rowid column (unless the TABLE is defined with the WITHOUT ROWID phrase).
You may find reading SQLite Autoincrement helpful.
edited Nov 26 '18 at 22:05
answered Nov 26 '18 at 21:59
MikeTMikeT
18.3k112844
18.3k112844
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
add a comment |
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
But i want to update data to a row where KEY_ID = MessageRecieverId
– user10643013
Nov 27 '18 at 16:36
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
So if i insert it using a random id then i wont be able to fetch the row as there is no foreign key so i have to identify only using primary key which is ID
– user10643013
Nov 27 '18 at 16:38
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
You are not inserting a random key. You are letting SQLite generate the key (id) (actually SQLite does this anyway, rather you are making that value more readily available by defining an alias) which will be unique and hence how you can identify any row. The value returned from the insert is that value. When you retrieve a row or rows (e.g. from a listview) then you include the id in the Cursor and thus have access to the id say when an item is clicked.
– MikeT
Nov 27 '18 at 19:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489427%2fandroid-database-sqlite-sqliteconstraintexception-unique-constraint-failed%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Because you are violating the database's constraint. The column
UserData.RecieversID
may only hold unique values which you seem to ignore in your code while updating.– f1sh
Nov 26 '18 at 21:43