【问题标题】:SQLiteConstraintException: error code 19: constraint failedSQLiteConstraintException:错误代码 19:约束失败
【发布时间】:2011-05-20 15:52:51
【问题描述】:

这里的初学者再次需要建议。尝试按照教程在数据库中添加/编辑/删除数据。

但是在尝试添加新标题时出现以下错误:

ERROR/Database(278): Error inserting Book_Title=testtitle Book_Author=testauthor

ERROR/Database(278): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed

我怀疑它对 ID 的约束,因为数据库已经在以前的类文件中创建。但是我对Java不够熟练,不知道如何修复它。编辑和删除数据工作正常。

一些代码:

DatabaseManager.class:

public void addRow(String rowStringOne, String rowStringTwo)
    {
        // this is a key value pair holder used by android's SQLite functions
        ContentValues values = new ContentValues();


        values.put(TABLE_ROW_ONE, rowStringOne);
        values.put(TABLE_ROW_TWO, rowStringTwo);


        // ask the database object to insert the new data 
        try{db.insert(TABLE_NAME, null, values);}
        catch(Exception e)
        {
            Log.e("DB ERROR", e.toString());
            e.printStackTrace();
        }
    }



private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
    {



        public CustomSQLiteOpenHelper(Context context)
        {
            super(context, DB_NAME, null, DB_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            // This string is used to create the database.  It should
            // be changed to suit your needs.
            String newTableQueryString = "create table " +
                                        TABLE_NAME +
                                        " (" +
                                        TABLE_ROW_ID + " integer primary key autoincrement not null," +
                                        TABLE_ROW_ONE + " text," +
                                        TABLE_ROW_TWO + " text" +
                                        ");";




            // execute the query string to the database.
            db.execSQL(newTableQueryString);

        }


        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            // NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
            // OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
        }
    } 
}

AddData.class:

private void addRow()
{
    try
    {
        // ask the database manager to add a row given the two strings
        db.addRow
        (

                textFieldOne.getText().toString(),
                textFieldTwo.getText().toString()

        );

        // request the table be updated
        updateTable();

        // remove all user input from the Activity
        emptyFormFields();
    }
    catch (Exception e)
    {
        Log.e("Add Error", e.toString());
        e.printStackTrace();
    }
}

【问题讨论】:

    标签: java android sqlite insert


    【解决方案1】:

    您被告知要插入由于主键或外键/键限制而无法插入的记录。您很可能想插入已在表中的带有ID 的记录。
    SQLIte AFAIK 中没有autoincrement 关键字,但任何表中都有_ID 属性,我建议您将其用作主键自动增量,而不是自己创建。

    发生异常的原因是您没有插入自定义主键,并且它不是您想象的自动增量。

    【讨论】:

    • 我尝试删除:TABLE_ROW_ID + " integer primary key autoincrement not null," + 但是它似乎没有任何区别,并且存在相同的错误。我将如何使用表中已存在的 ID 插入记录? (我已经尝试了各种)
    • 如果记录已经存在于表中,你更新它而不是插入它。插入用于创建新条目。
    • 该记录在表中尚不存在。数据库已经存在(它是在此之前创建的类文件,在应用程序启动时)
    • 按照您说的删除自定义主键后,您是否重新创建了表?
    • 是的。我相信无论如何我都可以解决部分错误。由于数据表单“正在进行中”,我试图插入 2 个字段,共 6 个。当我插入所有 6 个字段时,错误消失了(并且我收到了一个关于游标的新错误)。所以这似乎是问题所在。
    【解决方案2】:

    尝试删除并重新创建您的数据库。

    我没有发现您的代码有任何问题,我怀疑表定义的更改导致数据库和代码不同步

    (我的 sqlite 代码不以分号结尾,你也可以试试这个)

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 1970-01-01
      相关资源
      最近更新 更多