SQLite插入多次添加相同的条目

时间:2014-11-01 16:03:24

标签: android sqlite

我不确定为什么但是我的数据库中的每个项目都重复了大约30次,即使我只插入一次项目。我想也许这可能是因为walkdir()方法在指定的根文件夹中为每个文件插入了.mp3扩展名,或者可能是因为我对ListView回收的天真原因。

public class SQLHelper extends SQLiteOpenHelper{
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "MusicDatabase.db";
    private static final String SQL_CREATE_TABLE =
            "CREATE TABLE "+
                    FeedEntry.TABLE_NAME+" ("+
                    FeedEntry._ID+" INTEGER PRIMARY KEY,"+
                    FeedEntry.KEY_FILENAME+" TEXT UNIQUE,"+
                    FeedEntry.KEY_SONGNAME+" TEXT )";
    private static final String SQL_DELETE_TABLE = "DROP TABLE IF EXISTS "+ SQLContract.FeedEntry.TABLE_NAME;

    public SQLHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_TABLE);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(SQL_DELETE_TABLE);
        onCreate(db);
    }
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }/*
    public List<Song> getAllSongs(){
        List<Song> songs = new LinkedList<Song>();
        String query = "SELECT * FROM "+FeedEntry.TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);
        Song song = null;
        if(cursor.moveToFirst()){
            do {
                song = new Song();
                song.setId(Integer.parseInt(cursor.getString(0)));
                song.setFileName(cursor.getString(1));
                song.setSongName(cursor.getString(2));
                songs.add(song);
            }while(cursor.moveToNext());
        }
        return songs;
    }*/
    public void addSong(File file){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(FeedEntry.KEY_FILENAME, file.getPath());
        values.put(FeedEntry.KEY_SONGNAME, file.getName());
        db.insert(FeedEntry.TABLE_NAME,null,values);
    }
    public Cursor read(String sort, String direction){
        SQLiteDatabase db = this.getReadableDatabase();
        String[] projection = {
            FeedEntry._ID,
            FeedEntry.KEY_FILENAME,
            FeedEntry.KEY_SONGNAME,
        };
        String sortOrder = sort+direction;
        Cursor c = db.query(
                FeedEntry.TABLE_NAME,
                projection,null,null,null,null,
                sortOrder
        );
        return c;
    }
}

2 个答案:

答案 0 :(得分:0)

我认为问题不会发生在你的数据库代码上。

我觉得,你的问题与How ListView's recycling mechanism works

非常相似

正确处理ListView需要做大量的工作。

答案 1 :(得分:0)

经过多次试验和失败后,我发现我的代码中存在错误:

public static final String DATABASE_NAME = "MusicDatabase.db";

应该是:

public static final String DATABASE_NAME = "MusicDatabase";

我不知道为什么我认为数据库扩展是必要的。

相关问题