我正在编程小型应用程序,我为此留出了空间。 SQLite包装器。 据我所知,代码中有些功能无法使用。此外,文档似乎非常不完整,或者有时甚至是错误的。例如,dao批注@Update和@Delete。 我对使用此框架感到困惑,我想回到使用SQLite API来代替。您如何看待房间?
现在我要问的主要问题。我将向您展示我的代码,也许您会看到为什么无法正常工作。
首先,我的实体和刀。它应该代表一个音频文件。
from sqlalchemy import MetaData
from sqlalchemy.schema import CreateColumn
from sqlalchemy.ext.compiler import compiles
@compiles(CreateColumn, 'firebird')
def use_identity(element, compiler, **kw):
text = compiler.visit_create_column(element, **kw)
text = text.replace("SERIAL", "INT GENERATED BY DEFAULT AS IDENTITY")
return text
m = MetaData()
t = Table(
't', m,
Column('id', Integer, primary_key=True),
Column('data', String)
)
那是常规的数据库管理器。
@Entity(tableName = "audiofile")
public class AudioFile
{
@PrimaryKey(autoGenerate = true)
private long id;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "artist")
private String artist;
@ColumnInfo(name = "path")
private String path;
@ColumnInfo(name = "duration")
private long duration;
@Dao
public interface AudioFileDao
{
@Query("SELECT * FROM audiofile WHERE title LIKE :title")
AudioFile getAudioFile(String title);
@Query("SELECT * FROM audiofile")
List<AudioFile> getAll();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void initAudioFiles(AudioFile... audioFiles);
@Update
void updateAudioFile(AudioFile... audioFiles);
// @Query("DELETE FROM audiofile WHERE id = :audioFileId")
@Delete
void deleteAudioFile(AudioFile audioFile);
@Insert
void insertAll(AudioFile... audioFiles);
@Insert
long insertAudioFile(AudioFile audioFile);
}
Aaa这是我的测试,引起了我的所有担忧:
@Database(entities = {AudioFile.class}, version = 1)
public abstract class DbManager extends RoomDatabase
{
private static DbManager INSTANCE;
public abstract AudioFile.AudioFileDao audioFileDao();
public static DbManager getDbManager(Context ctx){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(ctx.getApplicationContext(), DbManager.class, "playlist").build();
}
return INSTANCE;
}
public static void destroyInstance(){
INSTANCE = null;
}
}
预先感谢您的帮助。这东西使我发疯。
答案 0 :(得分:0)
现在我要问的主要问题。我将向您展示我的代码,也许您会看到为什么无法正常工作。
我相信您的问题是,当您插入AudioFile时,未设置ID,因此在AudioFile对象audioFile中将其设置为0。但是,插入时,id将自动生成,并且不会为0,而是可能是某个时间插入的AudioFile对象的数量+1。
如果未设置 id 来使用同一AudioFile对象进行更新/删除,则会尝试使用ID 0 来更新删除。 不会有这样的行。
一个修复程序,假设AudioFile存在 setId 方法,但是可以使用:-
audioFile.setId(audioFileDao.insertAudioFile(audioFile));
可以处理不会导致异常的插入失败的修补程序可能是:-
long insertedId = audioFileDao.insertAudioFile(audioFile);
if (insertedId > 0) {
audioFile.setId(insertedId);
} else {
//......... handle not inserted
}
下面的代码在运行时显示上面的内容(您的Entity和Dao已被原样复制):-
...... database built
mAudioFileDao = mAppDB.audioFileDao();
AudioFile a = new AudioFile();
a.setTitle("MySong");
a.setDuration(5);
a.setArtist("Fred");
a.setPath("/thepath");
logAudioFile(a,"Before Insert into DB");
long currentId = mAudioFileDao.insertAudioFile(a); //<<<<<<<<<< INSERT INTO DB
logAudioFile(a,"Immediately after Insert. ID returned from insert is " + currentId);
a.setId(currentId); //<<<<<<<<<< SET the the id of the AudioFile object a
logAudioFile(a,"After setting the ID to " + currentId);
mAudioFileDao.updateAudioFile(a);
List<AudioFile> audioFileList = mAudioFileDao.getAll();
for (AudioFile af: audioFileList) {
logAudioFile(af,"Extracted from DB");
}
mAudioFileDao.deleteAudioFile(a);
Log.d("AUDIOFILEINFO","Attempt to delete Audio File undertaken");
audioFileList = mAudioFileDao.getAll();
for (AudioFile af: audioFileList) {
logAudioFile(af,"After deletion");
}
logAudioFile 方法为:-
private void logAudioFile(AudioFile a, String extra) {
Log.d(
"AUDIOFILEINFO",
"Title is " + a.getTitle() +
" ID is " + a.getId() +
"\n\tExtra Info is " + extra
);
}
2019-10-06 11:03:39.757 D/AUDIOFILEINFO: Title is MySong ID is 0
Extra Info is Before Insert into DB
2019-10-06 11:03:39.808 D/AUDIOFILEINFO: Title is MySong ID is 0
Extra Info is Immediately after Insert. ID returned from insert is 1
2019-10-06 11:03:39.808 D/AUDIOFILEINFO: Title is MySong ID is 1
Extra Info is After setting the ID to 1
2019-10-06 11:03:39.829 D/AUDIOFILEINFO: Title is MySong ID is 1
Extra Info is Extracted from DB
2019-10-06 11:03:39.831 D/AUDIOFILEINFO: Attempt to delete Audio File undertaken
您如何看待房间?
那是在征求意见,话题也在外。