java - Can't add more than one record, ID not autoincrimenting -


i'm trying add site details database , after insert row, output result textview. record being inserted database because it's being shown in textview, can insert 1 record , i'm not sure why. i'm been using tutorial here , modifying meet needs.

here dbadapter:

public class dbadapter {      // ///////////////////////////////////////////////////////////////////     // constants & data     // ///////////////////////////////////////////////////////////////////     // logging:     private static final string tag = "dbadapter";      // db fields     public static final string key_rowid = "_id";     public static final int col_rowid = 0;     /*      * change 1:      */     // todo: setup fields here:     public static final string key_name = "name";     public static final string key_address = "address";     public static final string key_username = "username";     public static final string key_password = "password";     public static final string key_port = "port";      // todo: setup field numbers here (0 = key_rowid, 1=...)     public static final int col_name = 1;     public static final int col_address = 2;     public static final int col_username = 3;     public static final int col_password = 4;     public static final int col_port = 5;      public static final string[] all_keys = new string[] { key_rowid, key_name,             key_address, key_username, key_password, key_port };      // db info: it's name, , table using (just one).     public static final string database_name = "sites";     public static final string database_table = "sitetable";     // track db version if new version of app changes format.     public static final int database_version = 2;      private static final string database_create_sql = "create table "             + database_table             + " ("             + key_rowid             + " integer primary key autoincrement, "              /*              * change 2:              */             // todo: place fields here!             // + key_{...} + " {type} not null"             // - key column name created above.             // - {type} 1 of: text, integer, real, blob             // (http://www.sqlite.org/datatype3.html)             // - "not null" means required field (must given             // value).             // note: must comma separated (end of line!) last 1 must             // have no comma!!             + key_name + " string not null, " + key_address             + " string not null, " + key_username + " string not null, "             + key_password + " string not null, " + key_port             + " integer not null"              // rest of creation:             + ");";      // context of application uses us.     private final context context;      private databasehelper mydbhelper;     private sqlitedatabase db;      // ///////////////////////////////////////////////////////////////////     // public methods:     // ///////////////////////////////////////////////////////////////////      public dbadapter(context ctx) {         this.context = ctx;         mydbhelper = new databasehelper(context);     }      // open database connection.     public dbadapter open() {         db = mydbhelper.getwritabledatabase();         return this;     }      // close database connection.     public void close() {         mydbhelper.close();     }      // add new set of values database.     public long insertrow(string name, string address, string username,             string password, int port) {         /*          * change 3:          */         // todo: update data in row new fields.         // todo: change function's arguments need!         // create row's data:         contentvalues initialvalues = new contentvalues();         initialvalues.put(key_name, name);         initialvalues.put(key_address, address);         initialvalues.put(key_username, username);         initialvalues.put(key_password, password);         initialvalues.put(key_port, port);         // insert database.         return db.insert(database_table, null, initialvalues);     }      // delete row database, rowid (primary key)     public boolean deleterow(long rowid) {         string = key_rowid + "=" + rowid;         return db.delete(database_table, where, null) != 0;     }      public void deleteall() {         cursor c = getallrows();         long rowid = c.getcolumnindexorthrow(key_rowid);         if (c.movetofirst()) {             {                 deleterow(c.getlong((int) rowid));             } while (c.movetonext());         }         c.close();     }      // return data in database.     public cursor getallrows() {         string = null;         cursor c = db.query(true, database_table, all_keys, where, null, null,                 null, null, null);         if (c != null) {             c.movetofirst();         }         return c;     }      // specific row (by rowid)     public cursor getrow(long rowid) {         string = key_rowid + "=" + rowid;         cursor c = db.query(true, database_table, all_keys, where, null, null,                 null, null, null);         if (c != null) {             c.movetofirst();         }         return c;     }      // change existing row equal new data.     public boolean updaterow(long rowid, string name, string address,             string username, string password, string port) {         string = key_rowid + "=" + rowid;          /*          * change 4:          */         // todo: update data in row new fields.         // todo: change function's arguments need!         // create row's data:         contentvalues newvalues = new contentvalues();         newvalues.put(key_name, name);         newvalues.put(key_address, address);         newvalues.put(key_username, username);         // newvalues.put(key_password, password);         // newvalues.put(key_port, port);          // insert database.         return db.update(database_table, newvalues, where, null) != 0;     }      // ///////////////////////////////////////////////////////////////////     // private helper classes:     // ///////////////////////////////////////////////////////////////////      /**      * private class handles database creation , upgrading. used      * handle low-level database access.      */     private static class databasehelper extends sqliteopenhelper {         databasehelper(context context) {             super(context, database_name, null, database_version);         }          @override         public void oncreate(sqlitedatabase _db) {             _db.execsql(database_create_sql);         }          @override         public void onupgrade(sqlitedatabase _db, int oldversion, int newversion) {             log.w(tag, "upgrading application's database version "                     + oldversion + " " + newversion                     + ", destroy old data!");              // destroy old database:             _db.execsql("drop table if exists " + database_table);              // recreate new database:             oncreate(_db);         }     } } 

i'm querying database here:

public class ftpconnector extends activity { dbadapter mydb;     public void oncreate(bundle icicle) {             super.oncreate(icicle);             setcontentview(r.layout.ftp);              status = (textview) findviewbyid(r.id.status);             editaddress = (edittext) findviewbyid(r.id.editaddress);             edituser = (edittext) findviewbyid(r.id.editusername);             editpassword = (edittext) findviewbyid(r.id.editpassword);             addsitebtn = (button) findviewbyid(r.id.addsitebtn);             addsitebtn.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     sitemanager();                  }             });             opendb();         }          private void opendb() {             mydb = new dbadapter(this);             mydb.open();          }          @override         protected void ondestroy() {             // todo auto-generated method stub             super.ondestroy();             closedb();         }          private void closedb() {             mydb.close();         }          //where insertrecord() happens             public void sitemanager() {                     final alertdialog customdialog = new alertdialog.builder(this).create();                     layoutinflater layoutinflater = (layoutinflater) getapplicationcontext()                             .getsystemservice(context.layout_inflater_service);                     view view = layoutinflater.inflate(r.layout.site_manager, null);                     final edittext tmpname = (edittext) view                             .findviewbyid(r.id.dialogsitename);                     final edittext tmpaddress = (edittext) view                             .findviewbyid(r.id.dialogaddress);                     final edittext tmpuser = (edittext) view                             .findviewbyid(r.id.dialogusername);                     final edittext tmppass = (edittext) view                             .findviewbyid(r.id.dialogpassword);                     final edittext tmpport = (edittext) view.findviewbyid(r.id.dialogport);                     final textview tmpsites = (textview) view.findviewbyid(r.id.textview6);                     final checkbox tmppassive = (checkbox) view                             .findviewbyid(r.id.dialogpassive);                     final button tmpclose = (button) view.findviewbyid(r.id.closebtn);                     final button tmptestbtn = (button) view.findviewbyid(r.id.testbtn);                     final button tmpsavetsite = (button) view.findviewbyid(r.id.savesite);                      customdialog.setview(tmpclose);                     customdialog.setview(tmptestbtn);                     customdialog.setview(tmpname);                     customdialog.setview(tmpaddress);                     customdialog.setview(tmpuser);                     customdialog.setview(tmppass);                     customdialog.setview(tmpport);                     customdialog.setview(tmppassive);                     customdialog.setview(tmpsavetsite);                     customdialog.setview(tmpsites);                      tmpclose.setonclicklistener(new onclicklistener() {                          @override                         public void onclick(view v) {                             customdialog.dismiss();                         }                     });                      tmptestbtn.setonclicklistener(new onclicklistener() {                          @override                         public void onclick(view v) {                             _name = tmpname.gettext().tostring();                             _address = tmpaddress.gettext().tostring();                             _user = tmpuser.gettext().tostring();                             _pass = tmppass.gettext().tostring();                             _port = integer.parseint(tmpport.gettext().tostring());                              _passive = false;                             if (tmppassive.ischecked()) {                                 _passive = true;                             }                              boolean status = ftpconnect(_address, _user, _pass, _port);                              if (status == true) {                                 toast.maketext(ftpconnector.this, "connection succesful",                                         toast.length_long).show();                             } else {                                 toast.maketext(ftpconnector.this,                                         "connection failed:" + status, toast.length_long)                                         .show();                              }                         }                     });                      tmpsavetsite.setonclicklistener(new onclicklistener() {                          @override                         public void onclick(view v) {                             tmpsites.settext("");                             string msg = "!";                             _name = tmpname.gettext().tostring();                             _address = tmpaddress.gettext().tostring();                             _user = tmpuser.gettext().tostring();                             _pass = tmppass.gettext().tostring();                             _port = integer.parseint(tmpport.gettext().tostring());                              long newid = mydb.insertrow(_name, _address, _user, _pass, 21);                              cursor c = mydb.getallrows();                              if (c.movetofirst()) {                                 int id = c.getint(0);                                 string _name = c.getstring(1);                                 string _address = c.getstring(2);                                 string _user = c.getstring(3);                                 string _pass = c.getstring(4);                                 int _port = c.getint(5);                                  msg += "id=" + id + "\n";                                 msg += ", name=" + _name + "\n";                                 msg += ", address=" + _address + "\n";                                 msg += ", username=" + _user + "\n";                                 msg += ", password=" + _pass + "\n";                                 msg += ", port=" + _port + "\n";                                  while (c.movetonext());                             }                             c.close();                             // displaytext(msg);                             tmpsites.settext(msg);                         }                     });                     customdialog.setview(view);                     customdialog.show();                 } 

why can't add more 1 record?

in here :

 while (c.movetonext());  //<<< 

currently not using loop do-while iterating through cursor(you forget add block while). data cursor using loop:

//more first row  c.movetofirst();  //iterate on rows  (int = 0; < c.getcount(); i++) {      // data here current row..      //move next row     c.movetonext();  } //close cursor c.close(); 

and using do-while can values current row as:

  c.movetofirst();   //more first row     {         // data here current row..      } while (c.movetonext()); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -