java - SQLite 约束异常 : error code 19: constraint failed

标签 java android sqlite insert

又是初学者,求指教。尝试按照教程向数据库添加/编辑/删除数据。

但是,我在尝试添加新标题时遇到以下错误:

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

最佳答案

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

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

关于java - SQLite 约束异常 : error code 19: constraint failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074463/

相关文章:

android - Android 上带有彩色边框的 Xamarin Forms Entry

objective-c - 核心数据-访问模拟器创建的sqlite文件?

java - VelocyPack VPackParser 从 toJson 方法返回 null _key

java - 类型不匹配 : cannot convert from boolean to int

java - 如何自定义Spring Boot标语?

java - 真的需要后退按钮吗?

java - 管辖权政策文件未由受信任的签名者签名......?

android - 复选框未出现在自定义 ListView 中

android - Cordova SQLite UTF8 在插入时抛出错误

iphone - 如何合并Sqlite数据库中按日期排序的2个表的数据?