Android SQLite如何使用Foreign KEY插入值

时间:2015-10-28 11:22:14

标签: android sqlite

我在database创建了一个SQLite Androidtable Playlist_Songs在两个表上使用foreign keys:{ {1}}和Songs

如何使用其他表中的外键添加值,因为Playlists在我的情况下不起作用。

values.put(playlists_songs_playlistid, "(SELECT "+playlists_id+" FROM "+TABLE_PLAYLISTS+" WHERE "+playlists_name+"="+playlist+")");

}

1 个答案:

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