我试图在android中将数据插入数据库。
这个有效:
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;
}
但是当我尝试为序列号添加一列时(因为我没有任何主键列),插入时出错。
这是新代码(不工作):
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
"create table study " +
"(serial integer primary key,percentage text,videoDuration text,totalTime text,date text)"
);
}
public boolean insertStudy(String percentage, String videoDuration, String totalTime, String date) {
serialNumber++;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("serial", serialNumber);
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)
我不明白为什么先工作而不是第二工作。我想了解为什么第二个代码不起作用。
我的应用程序可以使用第一个代码但是可能会在将来导致错误,所以我想使用第二个代码。
答案 0 :(得分:1)
串行列仅接受唯一值。
只需更新
db.execSQL("create table study (serial integer,percentage text,videoDuration text,totalTime text,date text)");
答案 1 :(得分:0)
百分比不能用作主键,如建议它应该是唯一的错误,建议的方法是添加PositionChanged -= ...
字段。
id
答案 2 :(得分:0)
尝试一下,添加自动增量并手动删除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;
}
答案 3 :(得分:0)
创建表时,将id和它作为主键自动增量:
db.execSQL("create table study (id integer primary key autoincrement, serial integer,percentage text,videoDuration text,totalTime text,date text)"
);