Thursday 15 April 2010

android - Item does not get added to the database -



android - Item does not get added to the database -

i have sqlite database bundled application asset, on first start of application database copied device below code.

public class databasehelper extends sqliteopenhelper { // android's default scheme path of application database. private static string db_path = "/data/data/my.apps.namespace/databases/"; private static string db_name = "db.sqlite"; private sqlitedatabase mydatabase; private final context mycontext; public databasehelper(context context) { super(context, db_name, null, 1); this.mycontext = context; } /** * creates empty database on scheme , rewrites own * database. * */ public void createdatabase() throws ioexception { boolean dbexist = checkdatabase(); if (dbexist) { // nil - database exist } else { // calling method , empty database created // default scheme path // of application gonna able overwrite // database our database. this.getreadabledatabase(); seek { copydatabase(); } grab (ioexception e) { throw new error("error copying database"); } } } /** * check if database exist avoid re-copying file each * time open application. * * @return true if exists, false if doesn't */ private boolean checkdatabase() { sqlitedatabase checkdb = null; seek { string mypath = db_path + db_name; checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly); } grab (sqliteexception e) { // database does't exist yet. } if (checkdb != null) { checkdb.close(); } homecoming checkdb != null ? true : false; } /** * copies database local assets-folder created * empty database in scheme folder, can accessed , * handled. done transfering bytestream. * */ private void copydatabase() throws ioexception { // open local db input stream inputstream myinput = mycontext.getassets().open(db_name); // path created empty db string outfilename = db_path + db_name; // open empty db output stream outputstream myoutput = new fileoutputstream(outfilename); // transfer bytes inputfile outputfile byte[] buffer = new byte[1024]; int length; while ((length = myinput.read(buffer)) > 0) { myoutput.write(buffer, 0, length); } // close streams myoutput.flush(); myoutput.close(); myinput.close(); } public void opendatabase() throws sqlexception { // open database string mypath = db_path + db_name; mydatabase = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite); } @override public synchronized void close() { if (mydatabase != null) mydatabase.close(); super.close(); } @override public void oncreate(sqlitedatabase db) { } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { } .... }

this seems work can access databases info illustration method:

public category getcategory(int id) { opendatabase(); cursor cursor = mydatabase.rawquery( "select * categories _id = " + id, null); cursor.movetofirst(); while (!cursor.isafterlast()) { category cat = cursortocategory(cursor); homecoming cat; } // create sure close cursor cursor.close(); homecoming null; }

however when want add together database not seem added, have below method

public void addtowordbank(word currentword) { opendatabase(); mydatabase.rawquery("insert wordbank(english) values(?) ", new string[] { currentword.english }); }

however when utilize below method check returned false,

public boolean wordbankhas(word currentword) { opendatabase(); cursor cursor = mydatabase.rawquery( "select * wordbank english language =? ", new string[] { currentword.english }); if (cursor.getcount() <= 0) { homecoming false; } homecoming true; }

there not seem errors in logs relating inserting etc.

you using rawquery() instead of execsql().

rawquery() works select queries. execsql() commands (i. e.: insert, update, delete, ...).

android sqlite

No comments:

Post a Comment