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

发布于 2024-11-08 06:24:27 字数 2795 浏览 0 评论 0原文

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

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

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

Beginner here again wanting advice. Trying to follow a tutorial for adding/edit/deleting data to/from the database.

However I'm getting the following error when trying to add a new title:

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

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

I suspect its a contraint on the ID, as the database is already created in a previous class file. However i'm not adept enough with Java to know how to fix it. Editing and Deleting data work fine.

Some code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笑百媚生 2024-11-15 06:24:27

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

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

You are told that you want to insert the record that cannot be inserted due to primary or foreign key/keys restrictions. Most likely you wanna insert the record with ID that has already been in the table.
There's no autoincrement keyword in SQLIte AFAIK, but there's an _ID attribute in any table, i recommend you to use it as primary key autoincrement instead of creating your own.

The exception occurs cause you don't insert your custom primary key and it's not autoincrement as you suppose.

虐人心 2024-11-15 06:24:27

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

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

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

Try to delete and recreate your database.

I do not see anything wrong in your code, I suspect a change in the table definition which makes database and code out of sync

(my code with sqlite does NOT end with semicolon ;, you may try this, too)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文