我在database
创建了一个SQLite
Android
,table
Playlist_Songs
在两个表上使用foreign keys
:{ {1}}和Songs
如何使用其他表中的外键添加值,因为Playlists
在我的情况下不起作用。
values.put(playlists_songs_playlistid, "(SELECT "+playlists_id+" FROM "+TABLE_PLAYLISTS+" WHERE "+playlists_name+"="+playlist+")");
}
答案 0 :(得分:3)
ContentValues
仅支持普通值,而不支持SQL表达式。
您可以使用query()
单独执行查询查询,也可以使用execSQL
执行自己的SQL:
db.execSQL("INSERT INTO "+TABLE_PLAYLISTS_SONGS+
"("+playlists_songs_playlistid+","+
playlists_songs_songid+")"+
"VALUES((SELECT "+playlists_id+
" FROM "+TABLE_PLAYLISTS+
" WHERE "+playlists_name+"=?),"+
"(SELECT "+songs_id+
" FROM "+TABLE_SONGS+
" WHERE "+songs_name+"=?))",
new Object[]{ playlist, song });