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