【问题标题】:SQLiteConstraintException when inseting to database插入数据库时​​出现 SQLiteConstraintException
【发布时间】:2016-08-27 14:10:17
【问题描述】:

我尝试将数据插入到 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)

我不明白为什么第一个有效但第二个无效。我想了解为什么第二个代码不起作用。

我的应用程序可以使用第一个代码,但将来可能会导致错误,所以我想使用第二个代码。

【问题讨论】:

  • 这很常见,因为您将百分比作为主键,并且当您插入数据时,主键会重复,最好将 Base._ID 作为主键,它将自动递增,如果您想使用你自己的,然后确保它们没有被重复。
  • 谷歌搜索你的异常和错误比在做任何事情之前把它发布到 StackOverflow 上更快更好。 A. 您将避免投反对票,B. 这将是您改进搜索查询的方式。
  • @Sufian:感谢您的回复。我还在学习,我会确保在代码中使用异常来更好地理解我的错误。但我的计数器每次都会更新。而且我猜它每次提供数据插入数据库时​​都是独一无二的。
  • @Rajkrishna 如果您事先在表中有数据,它将不起作用,因为 serial 的初始值将已分配给某些行。您选择的答案会很好用,因为它使用的是AUTOINCREMENT

标签: android android-sqlite sql-insert constraintexception


【解决方案1】:

序列列只接受唯一值。

只需更新

db.execSQL("create table study (serial integer,percentage text,videoDuration text,totalTime text,date text)");

【讨论】:

    【解决方案2】:

    试试吧,添加自动增量并手动删除 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】:

      百分比不能用作主键,如错误提示它应该是唯一的,推荐的方法是添加id字段。

      db.execSQL(
      "create table study " +
      "(id integer primary key, percentage,videoDuration text,totalTime text,date text)"
      );
      

      【讨论】:

      • 如果你不介意可以看看这个question....
      【解决方案4】:

      在创建表时将 id 作为主键自动递增:

         db.execSQL("create table study (id integer primary key autoincrement, serial integer,percentage text,videoDuration text,totalTime text,date text)"
      );
      

      【讨论】:

      • 谢谢,它现在可以工作了。我已删除序列号,因为存在 ID 以提供序列号。
      猜你喜欢
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多