Darko Petkovski
Darko Petkovski

Reputation: 3912

Inserting in database constraint failed

I have a problem with inserting data into tables. So here is what I'm doing. I'm adding a music album to favorites table, also adding the songs of that album to a different table called songs. So I get this from logCat:

04-08 13:19:58.846: E/SQLiteDatabase(6120): Error inserting album_id=1 mp4= mp3=http://nelu.burduja.com/musica/songs_with/with_515c34d1bbde7.mp3 title=Super hit artist=Gherghe Topa thumb=snwith_515c34d1aeee1.jpeg
04-08 13:19:58.846: E/SQLiteDatabase(6120): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at com.myapp.functions.DatabaseHelper.addSongs(DatabaseHelper.java:179)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at com.myapp.functions.DatabaseHelper.addFavorite(DatabaseHelper.java:215)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at com.myapp.functions.AlbumLazyAdapter$1.onClick(AlbumLazyAdapter.java:94)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.view.View.performClick(View.java:3511)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.view.View$PerformClick.run(View.java:14105)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.os.Handler.handleCallback(Handler.java:605)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.os.Looper.loop(Looper.java:137)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at android.app.ActivityThread.main(ActivityThread.java:4440)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at java.lang.reflect.Method.invokeNative(Native Method)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at java.lang.reflect.Method.invoke(Method.java:511)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
04-08 13:19:58.846: E/SQLiteDatabase(6120):     at dalvik.system.NativeStart.main(Native Method)

Firstly I insert an album:

public void addFavorite(Albums album) {

        ContentValues values = new ContentValues();
        values.put(ALB_GENRE, album.getTAG_GENRE());
        values.put(ALB_ID, album.getTAG_ID());
        values.put(ALB_MIX, album.getTAG_MIX());
        values.put(ALB_NAME, album.getTAG_NAME());
        values.put(ALB_SINGER, album.getTAG_SINGER());
        values.put(ALB_THUMB, album.getTAG_THUMB());
        // Inserting Row
        addSongs(album.getSongs(), album.getTAG_ID());
        database.insert(TABLE_FAVORITES, null, values);

    }

then i insert songs from that album via:

public void addSongs(ArrayList<Songs> arrayList, String albumID) {
        ContentValues values = new ContentValues();
        for (int i = 0; i < arrayList.size(); i++) {
            values.put("title", arrayList.get(i).getSONG_TITLE());
            values.put("artist", arrayList.get(i).getSONG_ARTIST());
            values.put("mp3", arrayList.get(i).getSONG_MP3());
            values.put("mp4", arrayList.get(i).getSONG_MP4());
            values.put("thumb", arrayList.get(i).getSONG_THUMB());
            values.put("album_id", albumID);
            database.insert("songs", null, values);
        }
    }

And finally my table queries:

private static final String DATABASE_CREATE_SONGS = "create table "
            + TABLE_SONGS + "(title text "
            + ", artist text, mp3 text, mp4 text, thumb text,"
            + " album_id text REFERENCES " + TABLE_FAVORITES + "(id));";

private static final String DATABASE_CREATE = "create table "
            + TABLE_FAVORITES + "(" + ALB_ID + " text primary key, " + ALB_NAME
            + " text, " + ALB_SINGER + " text, " + ALB_GENRE + " text, "
            + ALB_MIX + " text, " + ALB_THUMB + " text" + ");";

So can anybody help me!?

Upvotes: 2

Views: 652

Answers (3)

Xiao Liang
Xiao Liang

Reputation: 756

Ha, you've missed something really important, the base column _ID ! This must be INTEGER and NOT NULL. You can also assign it to AUTOINCREMENT.

Please view this site: https://developer.android.com/training/basics/data-storage/databases.html (Android developer guide about SQlite Databases)

And, this tutorial may also help you: Part 1: http://www.smashingmagazine.com/2010/10/25/get-started-developing-for-android-with-eclipse/

Part 2: http://www.smashingmagazine.com/2011/03/28/get-started-developing-for-android-with-eclipse-reloaded/

Upvotes: 0

acc15
acc15

Reputation: 1295

What you are doing?

You try to insert songs before FAVORITES? And songs should reference not existing FAVORITE?

Change the order...

From

    addSongs(album.getSongs(), album.getTAG_ID());
    database.insert(TABLE_FAVORITES, null, values);

To

    database.insert(TABLE_FAVORITES, null, values);
    addSongs(album.getSongs(), album.getTAG_ID());

Start using AUTOINCREMENT. I don't know where did you take values for album.getTAG_ID() but your solution is wrong in any case. Should be changed to something similar:

    final long id = database.insert(TABLE_FAVORITES, null, values);
    addSongs(album.getSongs(), id);

And table:

private static final String DATABASE_CREATE = "create table "
        + TABLE_FAVORITES + "(" + ALB_ID + " INTEGER primary key AUTOINCREMENT, " + ALB_NAME
        + " text, " + ALB_SINGER + " text, " + ALB_GENRE + " text, "
        + ALB_MIX + " text, " + ALB_THUMB + " text" + ");";

And method signature of course:

public void addSongs(ArrayList<Songs> arrayList, long albumID)...

Upvotes: 2

Arun C
Arun C

Reputation: 9035

Make sure that you didnot pass a null value into a column that you declare as not null

Upvotes: 0

Related Questions