java - Is this correct way to use Sqlite database in Android -


this question has answer here:

i beginner android. start learning it. creating register form integrated sqlite database. throwing error when click register button. please me. correct ? wrong code. how can debug application in android. using gennymotion emulator.

this database helper class : databasehelper.java

public class databasehelper extends sqliteopenhelper {      private static final int database_version = 1;     private static final string database_name = "contacts.db";     private static final string table_name = "contacts";     private static final string column_id = "id";     private static final string column_name = "name";     private static final string column_password = "password";     sqlitedatabase db;     private static final string table_create = "create table contacts (id integer primary key not null auto_increment,"+             "name text not null , password text not null);";      public databasehelper(context context){         super(context,database_name,null,database_version);     }      @override     public void oncreate(sqlitedatabase db) {         this.db = db;         db.execsql(table_create);     }      @override     public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {         string query = "drop table if exists "+table_name;         db.execsql(query);         this.oncreate(db);     }      public  void insertcontact(contact c)     {         db = getwritabledatabase();         contentvalues values = new contentvalues();         values.put(column_name,c.getname());         values.put(column_password,c.getpassword());         db.insert(table_name, null, values);         db.close();     }      public  string getusers()     {         string users = "";         db = this.getreadabledatabase();         string query = "select name,password "+table_name;         cursor cursor = db.rawquery(query,null);         if(cursor.movetofirst())         {             do{                 users = users + cursor.getstring(0)+":"+cursor.getstring(1)+" ";             }while(cursor.movetonext());         }         return users;     } } 

this register activity.

public class registeractivity extends activity {      databasehelper helper = new databasehelper(this);     button registerbtn;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.register);          registerbtn = (button)findviewbyid(r.id.btn_register);         registerbtn.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 registerbtnclick(v);             }         });     }      public void registerbtnclick(view v)     {         string name = ((edittext)findviewbyid(r.id.tf_name_register)).gettext().tostring();         string password = ((edittext)findviewbyid(r.id.tf_password_register)).gettext().tostring();         string confirm_password = ((edittext)findviewbyid(r.id.tf_confirm_password_register)).gettext().tostring();          if(name.isempty())         {             toast.maketext(getbasecontext(),"name should not empty",toast.length_long).show();         }         else if(password.isempty())         {             toast.maketext(getbasecontext(),"password required",toast.length_long).show();         }         else if(!password.equals(confirm_password))         {             toast.maketext(getbasecontext(),"password not match",toast.length_long).show();         }         else{             //save database              contact c = new contact();             c.setname(name);             c.setpassword(password);             helper.insertcontact(c);          }     } } 

please me when hit register button. throwing error. how can detect error , wrong code please ? start learning android.

here:

databasehelper helper = new databasehelper(this); 

this line causing issue because accessing activity context before creation of it.

use this or registeractivity.this inside other valid context.

declare helper @ class level , initialize inside oncreate method as:

 databasehelper helper ;//>> declare here @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.register);         // initialize `helper` here         helper = new databasehelper(this);        .... } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -