Existing DB - SQLite / android - read from db -


i tried many times use existing data base sqlite in android app ! can create new db, here , wanna use existing 1 ! codes , there's no problems there , not error , doesnot work , 1 can me whith ?

databasehelper.java:

public class databasehelper extends sqliteopenhelper {      private static string db_path = "/data/data/com.global.getrain/databases/";      private static string db_name = "bookdb";      private sqlitedatabase mydatabase;       private context mycontext;      public databasehelper(context context) {          super(context, db_name, null, 1);         this.mycontext = context;     }        public void createdatabase() throws ioexception{              boolean dbexist = checkdatabase();              if(dbexist){             }else{                  this.getreadabledatabase();                  try {                      copydatabase();                  } catch (ioexception e) {                      throw new error("error copying database");                  }             }          }          public boolean checkdatabase(){              sqlitedatabase checkdb = null;              try{                 string mypath = db_path + db_name;                 checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readonly);              }catch(sqliteexception e){               }              if(checkdb != null){                  checkdb.close();              }              return checkdb != null ? true : false;         }          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);              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_readonly);          }          @override         public synchronized void close() {                  if(mydatabase != null)                     mydatabase.close();                  super.close();          }        @override     public void oncreate(sqlitedatabase db) {         // sql statement create book table         string create_book_table = "create table books ( " + "id integer primary key autoincrement, " + "title text, "                 + "author text )";          // create books table         db.execsql(create_book_table);     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // drop older books table if existed         db.execsql("drop table if exists books");          // create fresh books table         this.oncreate(db);     }      // books table name     private static final string table_books = "books";      // books table columns names     private static final string key_id = "id";     private static final string key_title = "title";     private static final string key_author = "author";      private static final string[] columns = { key_id, key_title, key_author };      public void addbook(book book) {         log.d("addbook", book.tostring());         // 1. reference writable db         sqlitedatabase db = this.getwritabledatabase();          // 2. create contentvalues add key "column"/value         contentvalues values = new contentvalues();         values.put(key_title, book.gettitle()); // title         values.put(key_author, book.getauthor()); // author          // 3. insert         db.insert(table_books, // table                 null, // nullcolumnhack                 values); // key/value -> keys = column names/ values = column                             // values          // 4. close         db.close();     }      public book getbook(int id) {          // 1. reference readable db         sqlitedatabase db = this.getreadabledatabase();          // 2. build query         cursor cursor = db.query(table_books, // a. table                 columns, // b. column names                 " id = ?", // c. selections                 new string[] { string.valueof(id) }, // d. selections args                 null, // e. group                 null, // f. having                 null, // g. order                 null); // h. limit          // 3. if got results first 1         if (cursor != null)             cursor.movetofirst();          // 4. build book object         book book = new book();         book.setid(integer.parseint(cursor.getstring(0)));         book.settitle(cursor.getstring(1));         book.setauthor(cursor.getstring(2));          log.d("getbook(" + id + ")", book.tostring());          // 5. return book         return book;     }      // books     public list<book> getallbooks() {         list<book> books = new linkedlist<book>();          // 1. build query         string query = "select  * " + table_books;          // 2. reference writable db         sqlitedatabase db = this.getwritabledatabase();         cursor cursor = db.rawquery(query, null);          // 3. go on each row, build book , add list         book book = null;         if (cursor.movetofirst()) {             {                 book = new book();                 book.setid(integer.parseint(cursor.getstring(0)));                 book.settitle(cursor.getstring(1));                 book.setauthor(cursor.getstring(2));                  // add book books                 books.add(book);             } while (cursor.movetonext());         }          log.d("getallbooks()", books.tostring());          // return books         return books;     }      // updating single book     public int updatebook(book book) {          // 1. reference writable db         sqlitedatabase db = this.getwritabledatabase();          // 2. create contentvalues add key "column"/value         contentvalues values = new contentvalues();         values.put("title", book.gettitle()); // title         values.put("author", book.getauthor()); // author          // 3. updating row         int = db.update(table_books, // table                 values, // column/value                 key_id + " = ?", // selections                 new string[] { string.valueof(book.getid()) }); // selection                                                                 // args          // 4. close         db.close();          return i;      }      // deleting single book     public void deletebook(book book) {          // 1. reference writable db         sqlitedatabase db = this.getwritabledatabase();          // 2. delete         db.delete(table_books, key_id + " = ?", new string[] { string.valueof(book.getid()) });          // 3. close         db.close();          log.d("deletebook", book.tostring());      }  } 

mainactivity.java:

public class mainactivity extends activity {      private listview obj;      protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          databasehelper db = new databasehelper(this);         db = new databasehelper(this);           try {              db.createdatabase();          } catch (ioexception ioe) {              throw new error("unable create database");          }          try {              db.opendatabase();          } catch (sqlexception sqle) {              throw sqle;          }          /*          * db.addbook(new book("android application development cookbook",          * "wei meng lee")); db.addbook(new book(          * "android programming: big nerd ranch guide",          * "bill phillips , brian hardy")); db.addbook(new book(          * "learn android app development", "wallace jackson"));          *           *           * // books list<book> list = db.getallbooks();          *           * // delete 1 book db.deletebook(list.get(0));          *           * // books db.getallbooks();          */          obj = (listview) findviewbyid(r.id.listview1);          list<book> array_list = db.getallbooks();          arrayadapter<book> arrayadapter = new arrayadapter<book>(this, android.r.layout.simple_list_item_1, array_list);          obj.setadapter(arrayadapter);          db.close();      } 

ps : can publish book class if that's !

the issue in assets folder , must :

assets/databases/

i added databases folder work :d


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -