database - Can't Create Table With 4 Column SQLite Android Studio -


i very dumb newbie in android programming, here want make cashflow note app in android studio. in case want create table call category_table 4 column called categid, categname, note , currency. have doing tutorial google , etc extacly. till still have problem. when want insert data category_table, says no column named currency in android studio report. dont know do, have googling still cant solve problem. please gimme answer can understand newbie programmer. thanks. nb. here code: add_category code :

    public class addcategory extends actionbaractivity {     private static button btniadd;     private static button btnicancel;     edittext txtcategname, txtnote;     spinner selectcurrency;     arrayadapter<charsequence> adaptercurrency;     databasehelper mydb;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_add_category);         mydb = new databasehelper(this);         txtcategname = (edittext)findviewbyid(r.id.edittext);         txtnote = (edittext)findviewbyid(r.id.edittext2);         btnicancel = (button)findviewbyid(r.id.btncancel);         btniadd = (button)findviewbyid(r.id.btnadd);          //spinner         selectcurrency = (spinner) findviewbyid(r.id.spin_selectcurrency);         adaptercurrency = arrayadapter.createfromresource(this, r.array.currencyname,android.r.layout.simple_spinner_item );         adaptercurrency.setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item);         selectcurrency.setadapter(adaptercurrency);         selectcurrency.setonitemselectedlistener(new adapterview.onitemselectedlistener() {             @override             public void onitemselected(adapterview<?> parent, view view, int position, long id) {                 toast.maketext(getbasecontext(), parent.getitematposition(position)+" selected",toast.length_long).show();                 string currencyvalue = string.valueof(parent.getselecteditem());             }              @override             public void onnothingselected(adapterview<?> parent) {              }         });         addcategdata();     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_add_category, menu);         return true;     }      public void addcategdata(){         btniadd.setonclicklistener(                 new view.onclicklistener() {                     @override                     public void onclick(view v) {                         toast.maketext(addcategory.this,"clicked", toast.length_long).show();                       boolean isinserted =   mydb.insertcategdata(txtcategname.gettext().tostring(),                                 txtnote.gettext().tostring(), selectcurrency.getselecteditem().tostring());                         if (isinserted == true)                             toast.maketext(addcategory.this,"inserted", toast.length_long).show();                         else                             toast.maketext(addcategory.this,"not inserted", toast.length_long).show();                     }                 }         );         btnicancel.setonclicklistener(                 new view.onclicklistener() {                     @override                     public void onclick(view v) {                         finish();                     }                 }         );     }        @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item); } } 

and 1 databasehelper class

public class databasehelper extends sqliteopenhelper { public static final string myvillagesoftware = "myvillagesoftware"; public static final string database_name = "cashflow.db"; public static final string table_categ_name = "category_table"; public static final string col1 = "categid"; public static final string col2 = "categname"; public static final string col3 = "note"; public static final string col4 = "currency";    public databasehelper(context context) {     super(context, database_name, null, 1);   }  @override public void oncreate(sqlitedatabase db) {     db.execsql("drop table" + table_categ_name);     /*db.execsql("create table " + table_categ_name +             "(categid integer primary key autoincrement " +             "categname text" +             "note text" +             "currency text)");*/  }  public boolean insertcategdata(string categname, string note, string currency){     sqlitedatabase db = this.getwritabledatabase();     contentvalues contentvalues = new contentvalues();     contentvalues.put(col2, categname);     contentvalues.put(col3, note);     contentvalues.put(col4, currency);     long result = db.insert(table_categ_name, null, contentvalues);      if (result == -1)          return true;      else          return false;  } public arraylist<string>getallcategory(){     arraylist<string> allcategorylist = new arraylist<string>();     sqlitedatabase db = this.getreadabledatabase();     string selectcateg="select * " +table_categ_name;     cursor cursor = db.rawquery(selectcateg, null);     if(cursor.getcount()>0){         while (cursor.movetonext()){             string categname=cursor.getstring(cursor.getcolumnindex(col2));             allcategorylist.add(col2);          }return allcategorylist;     }      return allcategorylist;  }   @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {     db.execsql("drop table if exists " +table_categ_name);     oncreate(db); 

thanks before... :)

1) uninstall app , install again.

2) issue "no column named currency in android studio report" because column currency might added after table created. have define database version(an integer value e.g. ver=1) , change version of db whenever changes in database(increase int value e.g. ver=2) call onupgrade() , drop tables , create again. solve problem.

edit: have given database version 1 default in super(context,dbname,cursorfactory,dbversion)

public databasehelper(context context) {     super(context, database_name, null, 1); } 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -