我花了很多时间寻找答案,但我找不到。
我正在尝试在我的Android应用上使用SQLite数据库,但是当我尝试使用db.insert(object)
添加新对象时,我收到一条错误消息,指出我的主键不是唯一的。
我做错了什么?
SQLite数据库:
public class SqlCadastro extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contactsManager";
private static final String TABLE_MATERIAS = "Materias";
// Contacts Table Columns names
private static final String KEY_NOME = "Nome";
private static final String KEY_NOTA1 = "Tri1";
private static final String KEY_NOTA2 = "Tri2";
private static final String KEY_NOTA3 = "Tri3";
private static final String KEY_PRENOTA3 = "Est3Tri";
private static final String KEY_MA = "MA";
private static final String KEY_PREPFV = "EstPFV";
private static final String KEY_PFV = "PFV";
public SqlCadastro(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_MATERIAS + "("
+ KEY_NOME + " TEXT UNIQUE," + KEY_NOTA1 + " FLOAT," + KEY_NOTA2 + " FLOAT," + KEY_PRENOTA3 + " FLOAT,"
+ KEY_NOTA3 + " FLOAT," + KEY_MA + " FLOAT," + KEY_PREPFV + " FLOAT, " + KEY_PFV + " FLOAT, PRIMARY KEY (" + KEY_NOME + ") ON CONFLICT IGNORE)" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MATERIAS);
onCreate(db);
}
public void addMateria(Materias materia) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NOME, materia.getNome());
values.put(KEY_NOTA1, Float.toString(materia.getNota1()));
values.put(KEY_NOTA2, Float.toString(materia.getNota2()));
values.put(KEY_PRENOTA3, Float.toString(materia.getPreNota3()));
values.put(KEY_NOTA3, Float.toString(materia.getNota3()));
values.put(KEY_MA, Float.toString(materia.getMA()));
values.put(KEY_PREPFV, Float.toString(materia.getPrePFV()));
values.put(KEY_PFV, Float.toString(materia.getPFV()));
// Inserting Row
db.insertOrThrow(TABLE_MATERIAS, null, values);
db.close(); // Closing database connection
}
}
错误日志:
FATAL EXCEPTION: main
android.database.sqlite.SQLiteConstraintException: column Nome is not unique (code 19)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1365)
at com.testemedia.mediacp2.SqlCadastro.addMateria(SqlCadastro.java:68)
at com.testemedia.mediacp2.Cadastrar.onClick(Cadastrar.java:49)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
error opening trace file: No such file or directory (2)
答案 0 :(得分:3)
我做错了什么?
您正在向数据库表中添加一行,其中所需的主键已被另一个现有行使用。之一:
使用单独的列作为主键,例如添加_ID INTEGER PRIMARY KEY AUTOINCREMENT
或
确保您在Nome
列中放置的所有内容都是唯一的
答案 1 :(得分:1)
列Nome
(KEY_NOME
)被声明为唯一&作为主键(暗示唯一)。
确保Nome
中没有重复但唯一的值。