android - Databases from assets folder -


how data assets folder db? have .db file in assets folder. here doing.

dbhelper2.class

package com.example.testapp;  import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream;  import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteexception; import android.database.sqlite.sqliteopenhelper; import android.util.log;        public class databasehelper2 extends sqliteopenhelper{       private static string db_path = "/data/data/com.example.testapp/databases/";        private static string db_name = "birthdate_details.db";        private sqlitedatabase mydatabase;        private final context mycontext;        public databasehelper2(context context)        {           super(context, db_name, null, 1);           this.mycontext = context;       }        public void createdatabase() throws ioexception{       boolean dbexist = checkdatabase();        if(dbexist)       {           log.i("db....", "database available...."+db_name);       }       else       {           this.getwritabledatabase();            try {            copydatabase();            } catch (ioexception e) {            throw new error("error copying database");            }           log.i("db..", "database created.....");        }           }         public boolean checkdatabase(){        sqlitedatabase checkdb = null;        try{        string mypath = db_path + db_name;        checkdb = sqlitedatabase.opendatabase(mypath, null, sqlitedatabase.open_readwrite);        }catch(sqliteexception e){            log.e("checkdb","db not found");       //database does't exist yet.        if(checkdb != null){        checkdb.close();        }       }             {           if(checkdb != null){                checkdb.close();                }            this.close();       }       return checkdb != null ? true : false;        }           private void copydatabase() throws ioexception{        inputstream myinput = mycontext.getassets().open(db_name);        string outfilename = db_path + db_name;        outputstream myoutput = new fileoutputstream(outfilename);           byte[] buffer = new byte[1024];        int length;        while ((length = myinput.read(buffer))>0){        myoutput.write(buffer, 0, length);        }        myoutput.flush();        myoutput.close();        myinput.close();        }        public sqlitedatabase opendatabase() throws sqlexception{        string mypath = db_path + db_name;        return 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) {         }           public cursor getdata()       {           sqlitedatabase mydb ;           cursor cursor ;                  mydb=this.opendatabase();                                         cursor=mydb.rawquery("select * bdatedetails",null);                      return cursor;        }  } 

mainactivity.class

public class mainactivity extends activity {      arraylist<string> allassociateddata;     listview list;     databasehelper2 mydbhelper;     int pos;     cursor c;     arrayadapter<string> ad;  @override protected void oncreate(bundle savedinstancestate) {     // todo auto-generated method stub     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      list = (listview) findviewbyid(r.id.listview1);      allassociateddata = new arraylist<string>();     ad = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, allassociateddata);     list.setadapter(ad);      mydbhelper = new databasehelper2(this);      try      {         mydbhelper.createdatabase();         c = mydbhelper.getdata();          c.movetoposition(0);         allassociateddata.add(c.getstring(3));         system.out.println(c.getstring(3));         toast.maketext(getapplicationcontext(), c.getstring(3), 0).show();         //ad.notifydatasetchanged();      }catch (ioexception e) {            // todo auto-generated catch block            e.printstacktrace();            }          {         mydbhelper.close();     }  }    } 

but data not coming.

also, problem in internal db table.

login.java

public class login extends activity { edittext et1,et2; button b; textview tv1,tv2,tv3; mydatabase mdb; int count=1;       protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_login);         et1=(edittext) findviewbyid(r.id.edittext1);         et2=(edittext) findviewbyid(r.id.edittext2);         tv1=(textview) findviewbyid(r.id.textview1);         tv2=(textview) findviewbyid(r.id.textview2);         tv3=(textview) findviewbyid(r.id.textview3);         b=(button) findviewbyid(r.id.button1);         mdb = new mydatabase(this);         mdb.open();           b.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                  string username=et1.gettext().tostring();                 string password=et2.gettext().tostring();                 cursor c = mdb.getalluser();                 if(c != null){                     while(c.movetonext()==true)                     {   int id=c.getint(0);                         string uname = c.getstring(1);                         string pwd = c.getstring(2);                         long mob=c.getlong(3);                         string email = c.getstring(4);                         toast.maketext(getapplicationcontext(),""+id+","+uname+","+pwd+","+mob+","+email,0).show();                         if(count<=3)                         {                         if(username.equals(uname)&&password.equals(pwd))                         {                         intent in=new intent(getapplicationcontext(),welcome.class);                         in.putextra("key",uname);                         mdb.close();                         c.close();                         startactivity(in);                          return;                         }                         else                         {                          }                         }                         else                         {                             toast.maketext(getapplicationcontext(), "account blocked",0).show();                             }                      }                     tv3.settext("no of tries "+count);                     count++;                     c.close();                     toast.maketext(getapplicationcontext(), "username/password incorrect",0).show();                 }              }         }); 

mydatabase.java

public class mydatabase  { private myhelper mh; private sqlitedatabase sdb;  public mydatabase(context con) {     mh=new myhelper(con,"userdetails",null,1); } public void open() {   try{     sdb=mh.getwritabledatabase(); } catch(exception e) {  } } public void insertregiseruser(string username,string password,long mobile,string e_mail)  {     contentvalues cv=new contentvalues();     cv.put("uname",username);     cv.put("pwd",password);     cv.put("mob", mobile);     cv.put("email",e_mail);     sdb.insert("login", null,cv); }  public void updatepwd(string old_pwd,string username,string new_pwd) {     contentvalues cv=new contentvalues();     cv.put("pwd",new_pwd);     sdb.update("login", cv,"pwd=? , uname=?",new string[]{old_pwd,username}); }  public cursor getalluser() {   cursor c=sdb.query("login",null,null,null,null,null,null);     return c;  } class myhelper extends sqliteopenhelper {        public myhelper(context context, string name, cursorfactory factory,             int version) {         super(context, name, factory, version);      }      @override     public void oncreate(sqlitedatabase db) {         // todo auto-generated method stub         db.execsql("create table login(_id integer primary key, uname text unique, pwd text, mob long, email text);");      }     @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         // todo auto-generated method stub      }  } public void close(){     sdb.close(); } } 


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -