我在将数据插入表格时遇到问题。所以这就是我正在做的事情。我正在将音乐专辑添加到收藏夹表格,同时将该专辑的歌曲添加到另一个名为歌曲的表格中。所以我从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" + ");";
任何人都可以帮助我!?
答案 0 :(得分:2)
你在做什么?
您是否尝试在收藏前插入歌曲? 歌曲应该引用不存在的最喜欢的?
更改订单......
这
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)...
答案 1 :(得分:0)
确保未将null值传递给声明为非null的列
答案 2 :(得分:0)
请查看此站点: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/