我想在当前的pb中添加值,日期和详细信息。我在数据库中收到pbInfo的错误“冲突声明”。我该如何解决该错误?
@Entity(tableName = "pb_table")
data class Pb(@PrimaryKey
val pb: String)
@Entity
data class PbInfo(@PrimaryKey
var value: Double,
var date: Int,
var details: String)
@Dao
interface PbInfoDao {
@Insert
fun update(vararg pbInfo: PbInfo): LongArray
INSTANCE?.let { database ->
scope.launch {
populateDatabase(database.pbDao(), database.pbInfo())
}
}
}
suspend fun populateDatabase(pbDao: PbDao, pbInfoDao: PbInfoDao) {
pbDao.deleteAll()
var pb = Pb("Squat")
pbDao.insert(pb)
var pbInfo = PbInfo(122.5, 28, "I was feeling on top form today!")
答案 0 :(得分:1)
首先,您在一个类中有两个实体(可能是冲突)
因此,为单独的
if (Intent.ACTION_SEND.equals(action) && type != null) {
// Make sure the file is of type audio
if (type.startsWith("audio/")) {
handle(); // Handle audio being sent
}
} else if (Intent.ACTION_VIEW.equals(action) && type != null) {
// Make sure the file is of type audio
if (type.startsWith("audio/")) {
handle(); // Handle audio being sent
}
}
private void handle() {
Uri ur = intent.getParcelableExtra(Intent.EXTRA_TEXT);
String url = ur.getPath();
if (url == null) {
return;
}
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
// Select the following fields from an audio track in the storage
String[] projection = {MediaStore.Audio.AudioColumns._ID, MediaStore.Audio.AudioColumns.DATA,
MediaStore.Audio.ArtistColumns.ARTIST, MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AlbumColumns.ALBUM_ID,
MediaStore.Audio.AudioColumns.DURATION, MediaStore.Audio.Media.TRACK, MediaStore.Audio.Media.YEAR};
Cursor c = context.getContentResolver().query(uri, projection, selection, null, null);
if (c != null) {
while (c.moveToNext()) {
Long id = c.getLong(0);
String path = c.getString(1);
String artist = c.getString(2);
String title = c.getString(3);
String album = c.getString(4);
Long albumId = c.getLong(5);
Long duration = c.getLong(6);
int trackNumber = getTrackNumber(c.getString(7), 1);
int cdNumber = getTrackNumber(c.getString(7), 0);
int year = 0;
if (c.getString(8) != null) {
year = Integer.parseInt(c.getString(8));
}
if (path.equals(url)) {
// Add the data to the HashMap then return it.
audioTrack.put("id", id);
audioTrack.put("artist", artist);
audioTrack.put("title", title);
audioTrack.put("album", album);
audioTrack.put("albumId", albumId);
audioTrack.put("uri", path);
audioTrack.put("duration", formatDuration(duration));
audioTrack.put("artwork", getAlbumArt(context, albumId));
audioTrack.put("genre", getGenre(context, id));
audioTrack.put("trackNumber", trackNumber);
audioTrack.put("cdNumber", cdNumber);
audioTrack.put("year", year);
System.out.println("Audio track sent: " + audioTrack.get("title"));
}
}
// Always make sure to close the cursor after finishing its use
c.close();
}
}
添加单独的类。
然后,在RoomDatabase抽象类中,添加两个这样的实体类(并创建单独的Entity
接口类):
Dao
这应该解决实体类的冲突。我只有一个带有两个实体的数据库,并且没有任何问题。 (请注意我,因为我不知道Kotlin语法)
答案 1 :(得分:0)
使用此
@Insert(onConflict = OnConflictStrategy.REPLACE)
代替
@Insert