0

我正在编写一个音乐播放器,尽管在重新创建音乐数据库时使用了 songdb.close(),但我收到了这个错误。

UpdateDB.java:reloadDB():

public void reloadDB (View v) {
        status.setText(getText(R.string.updatedb_text_status_init));
        loadSongsFromFile();
        status.setText(getText(R.string.updatedb_text_status_recdb));
        this.getDatabasePath("songs").delete();
        SongsDBOpenHelper sdboh = new SongsDBOpenHelper(this.getApplicationContext());
        SQLiteDatabase songsdb = sdboh.getWritableDatabase();
        status.setText(getText(R.string.updatedb_text_status_fildb));
        songsdb.beginTransaction();
        for (String s : songs) {
            AudioFile af = new AudioFile(new File(s));
            songsdb.execSQL("INSERT OR IGNORE INTO artists (name) VALUES (\"" + af.getMetadata()[1] + "\")");
            Cursor c = songsdb.rawQuery("SELECT * FROM artists WHERE name = \"" + af.getMetadata()[1] + "\";", null);
            c.moveToFirst();
            songsdb.execSQL("INSERT OR IGNORE INTO albums (name, artist) VALUES (\"" + af.getMetadata()[2] + "\", \"" + c.getInt(0) + "\")");
            c = songsdb.rawQuery("SELECT * FROM albums WHERE name = \"" + af.getMetadata()[2] + "\";", null);
            c.moveToFirst();
            songsdb.execSQL("INSERT OR IGNORE INTO songs (name, album, file) VALUES (\"" + af.getMetadata()[0] + "\", \"" + c.getInt(0) + "\", \"" + af.getFileString() + "\")");
            Log.d("Song", af.getMetadata()[0]);
        }
        status.setText(getText(R.string.updatedb_text_status_finishing));
        songsdb.setTransactionSuccessful();
        songsdb.endTransaction();
        songsdb.close();
        sdboh.close();
        this.finish();
    }

SongsDBOpenHelper:onCreate():

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE songs (_id INTEGER PRIMARY KEY ASC, name VARCHAR(50) UNIQUE, album INTEGER, file VARCHAR(100));");
    db.execSQL("CREATE TABLE albums (_id INTEGER PRIMARY KEY ASC, name VARCHAR(50) UNIQUE, artist INTEGER);");
    db.execSQL("CREATE TABLE artists (_id INTEGER PRIMARY KEY ASC, name VARCHAR(50) UNIQUE);");
}

该类AudioFile不使用任何数据库。

而且我知道我用rawQuery错了方法...

编辑:

添加try/catch-blocks后,这个问题就解决了。但是对于每首现有歌曲,我都会遇到其他错误:

Failure 10 (Disk I/O error) on 0x1f5738 when executing 'INSERT OR IGNORE ...'

这是从哪里来的?

4

1 回答 1

0

我曾经遇到过这个错误。需要显式关闭的不是数据库。它是您从查询中获得的光标。

于 2013-11-07T20:44:47.167 回答