Android 插入数据库时的SQLiteConstraintException
我曾尝试在android中向数据库中插入数据 这一个有效:Android 插入数据库时的SQLiteConstraintException,android,android-sqlite,sql-insert,constraintexception,Android,Android Sqlite,Sql Insert,Constraintexception,我曾尝试在android中向数据库中插入数据 这一个有效: public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL( "create table study " + "(percentage integer primary key,videoDuration text,totalTime text,
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table study " +
"(percentage integer primary key,videoDuration text,totalTime text,date text)"
);
}
public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("percentage", percentage);
contentValues.put("videoDuration", videoDuration);
contentValues.put("totalTime", totalTime);
contentValues.put("date", date);
db.insert("study", null, contentValues);
return true;
}
但当我尝试为序列号再添加一列时(因为我没有主键列),我在插入时出错
这是新代码(不工作):
我使用serialNumber作为计数器,这样它将继续在串行中插入值,并充当主键。我收到此代码的错误信息:
Error inserting date=27/08/2016 percentage=2.6 serial=1 totalTime=0:0:1 videoDuration=22:96
android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
我不明白为什么第一个有效,而不是第二个。我想了解为什么第二个代码不起作用
我的应用程序可以使用第一个代码,但将来可能会导致错误,所以我想使用第二个代码。序列列只接受唯一值 只需更新
db.execSQL("create table study (serial integer,percentage text,videoDuration text,totalTime text,date text)");
百分比不能用作主键,就像错误建议的那样,它应该是唯一的,建议的方法是添加
id
字段
db.execSQL(
"create table study " +
"(id integer primary key, percentage,videoDuration text,totalTime text,date text)"
);
尝试添加自动增量并手动删除id设置:
db.execSQL(
"create table study " +
"(serial integer primary key AUTOINCREMENT ,percentage text,videoDuration text,totalTime text,date text)"
);
public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("percentage", percentage);
contentValues.put("videoDuration", videoDuration);
contentValues.put("totalTime", totalTime);
contentValues.put("date", date);
db.insert("study", null, contentValues);
return true;
}
public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("percentage", percentage);
contentValues.put("videoDuration", videoDuration);
contentValues.put("totalTime", totalTime);
contentValues.put("date", date);
db.insert("study", null, contentValues);
return true;
}
创建表时,将id和它作为主键自动递增:
db.execSQL("create table study (id integer primary key autoincrement, serial integer,percentage text,videoDuration text,totalTime text,date text)"
);
这很常见,因为您将百分比保留为主键,并且在插入数据时,主键会重复,最好将Base.\u ID作为主键,主键会自动递增,如果你想使用你自己的,那么确保它们没有被复制。在做任何事情之前,用谷歌搜索你的异常和错误比在StackOverflow上发布更快更好。A.你将避免否决票,B.这就是你将如何改进搜索查询的方法。@Sufian:可能重复@Sufian:谢谢你的回复。我还在学习,我会确保在代码中使用异常来更好地理解我的错误。但我的计数器每次都会更新。我猜每次它提供数据插入数据库时,它都是唯一的。@Rajkrishna如果您事先在表中有数据,它将不起作用,因为
serial
的初始值将被分配给某一行。您选择的答案将很好地工作,因为它正在使用AUTOINCREMENT
。谢谢,它现在可以工作了。我已经删除了序列号,因为现在的id是用来提供序列号的。如果你不介意的话,请看这个。。
db.execSQL("create table study (id integer primary key autoincrement, serial integer,percentage text,videoDuration text,totalTime text,date text)"
);