I am using RoomDatabase for uploading data to database. I was successful in uploading the data the first time.But the second time when I try to upload another data I got an exception.The problem is at contactDao.insert(contact); in the MainActivity.class. It is my first time using Room Database. Please shed some light on this issue.
我正在使用RoomDatabase将数据上传到数据库。我第一次成功上传数据。但是当我第二次尝试上传另一个数据时,我得到了一个例外。问题出在contactDao.insert(contact);在MainActivity.class中。这是我第一次使用Room Database。请详细说明这个问题。
Exception:
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: contact.index (code 1555)
#################################################################
Error Code : 1555 (SQLITE_CONSTRAINT_PRIMARYKEY)
Caused By : Abort due to constraint violation.
(UNIQUE constraint failed: contact.index (code 1555))
#################################################################
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:857)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
at android.arch.persistence.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
at com.globemaster.com.roomdatabase.ContactDao_Impl.insert(ContactDao_Impl.java:97)
at com.globemaster.com.roomdatabase.MainActivity$1.onClick(MainActivity.java:34)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Contact.class:
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;
@Entity(tableName = "contact")
public class Contact {
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@ColumnInfo(name = "index")
@PrimaryKey(autoGenerate = true)
private int index=1;
@ColumnInfo(name = "phnmbr")
@NonNull
private String phoneNumber;
@ColumnInfo(name = "firstnme")
private String firstname;
@ColumnInfo(name = "lastnme")
String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@NonNull
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(@NonNull String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
ContactDao(Interface):
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.Update;
import java.util.List;
@Dao
public interface ContactDao {
@Insert
public void insert(Contact contacts);
@Update
public void update(Contact contacts);
@Delete
public void delete(Contact contacts);
@Query("SELECT * FROM contact")
public List<Contact> getContacts();
@Query("SELECT * FROM contact WHERE `index`=:index" )
public Contact getContactWithIndex(int index);
}
AppDatabase.class:
import android.arch.persistence.room.Database;
import android.arch.persistence.room.RoomDatabase;
@Database(entities = {Contact.class},version = 1,exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
public abstract ContactDao getContactDAO();
}
MainActivity.java:
appdatabase= Room.databaseBuilder(MainActivity.this,AppDatabase.class,"db-contacts").allowMainThreadQueries().build();
contactDao=appdatabase.getContactDAO();
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Contact contact=new Contact();
contact.setFirstname(firstname.getText().toString());
contact.setLastname(lastname.getText().toString());
contact.setPhoneNumber(phonenumber.getText().toString());
contactDao.insert(contact);
Toast.makeText(getApplicationContext(),"Uploaded",Toast.LENGTH_SHORT).show();
}
});
1 个解决方案
#1
2
I don’t use Room, but as I can see, you have a autogenerated primary key, but you always set that to 1, remove the =1
from the Contact class and your problem should be solved :)
我不使用Room,但是正如我所看到的,你有一个自动生成的主键,但是你总是把它设置为1,从Contact类中删除= 1,你的问题应该解决了:)
Example:
@ColumnInfo(name = "index")
@PrimaryKey(autoGenerate = true)
private int index;
#1
2
I don’t use Room, but as I can see, you have a autogenerated primary key, but you always set that to 1, remove the =1
from the Contact class and your problem should be solved :)
我不使用Room,但是正如我所看到的,你有一个自动生成的主键,但是你总是把它设置为1,从Contact类中删除= 1,你的问题应该解决了:)
Example:
@ColumnInfo(name = "index")
@PrimaryKey(autoGenerate = true)
private int index;