2

我在将数据插入表时遇到问题。所以这就是我正在做的事情。我正在将音乐专辑添加到收藏夹表中,还将该专辑中的歌曲添加到称为歌曲的不同表中。所以我从 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)

首先我插入一张专辑:

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

    }

然后我通过以下方式插入该专辑中的歌曲:

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

最后我的表查询:

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" + ");";

那么有人可以帮帮我吗!?

4

3 回答 3

2

你在做什么?

您尝试在 FAVORITES 之前插入歌曲?和歌曲应该引用不存在的FAVORITE?

换个顺序...

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

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

开始使用AUTOINCREMENT。我不知道你在哪里取值,album.getTAG_ID()但无论如何你的解决方案都是错误的。应该改为类似的东西:

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

和表:

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" + ");";

当然还有方法签名:

public void addSongs(ArrayList<Songs> arrayList, long albumID)...
于 2013-04-08T11:52:54.527 回答
0

确保您没有将空值传递到您声明为非空的列中

于 2013-04-08T11:31:05.877 回答
0

哈,你错过了一些非常重要的东西,基本列 _ID !这必须是 INTEGER 而不是 NULL。您也可以将其分配给 AUTOINCREMENT。

请查看此站点:https ://developer.android.com/training/basics/data-storage/databases.html (关于 SQlite 数据库的 Android 开发人员指南)

而且,本教程还可以帮助您:第 1 部分:http ://www.smashingmagazine.com/2010/10/25/get-started-developing-for-android-with-eclipse/

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

于 2013-04-09T04:26:24.590 回答