SQLiteConstraintException:错误代码19:约束失败

blmhpbnm  于 2022-11-15  发布在  SQLite
关注(0)|答案(2)|浏览(452)

新手又来寻求建议了。尝试遵循向数据库添加数据/编辑数据/从数据库删除数据的教程。
但是,我在尝试添加新书目时遇到以下错误:

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();
    }
}
bkhjykvo

bkhjykvo1#

系统会告知您要插入由于主键或外键/键限制而无法插入的记录。最有可能的是,您希望插入表中已有的ID记录。
SQLIte AFAIK中没有autoincrement关键字,但任何表中都有_ID属性,我建议您将其用作主键自动增量,而不是创建自己的属性。
发生异常是因为您没有插入您的定制主键,并且它不是您所认为的自动递增。

v440hwme

v440hwme2#

尝试删除并重新创建数据库。
我没有看到您的代码中有任何错误,我怀疑是表定义中的更改导致数据库和代码不同步
(我的Sqlite代码不以分号结尾;,您也可以尝试一下)

相关问题