我正在使用RoomDatabase将数据上传到数据库。我第一次成功上传了数据。但是当我第二次尝试上传其他数据时,我遇到了异常。问题出在MainActivity.class中的 contactDao.insert(contact); 。这是我第一次使用Room Database。请详细说明这个问题。
例外:
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(接口):
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();
}
});
答案 0 :(得分:2)
我不使用Room,但是正如我所看到的,你有一个自动生成的主键,但是你总是把它设置为1,从Contact类中删除=1
,你的问题应该解决了:)
示例:
@ColumnInfo(name = "index")
@PrimaryKey(autoGenerate = true)
private int index;