Wednesday, 15 January 2014

android - SQLite insert is adding the same entry multiple times -



android - SQLite insert is adding the same entry multiple times -

i'm not sure why each item in database duplicated 30 times though insert each item once. thought perhaps might result of walkdir() method inserts database each file .mp3 extension within specified root folder or perhaps result of naivety regarding listview recycling.

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 * "+feedentry.table_name; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(query,null); song song = null; if(cursor.movetofirst()){ { 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()); } homecoming 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 ); homecoming c; } }

i think problem doesn't happen on database code.

i feel, problem quite similar how listview's recycling mechanism works

dealing listview correctly requires quite amount of work.

android sqlite

No comments:

Post a Comment