android - Trouble Parsing XML Into SQLite Database -


the application working on parses data .xml file , stores database. however, upon each application launch, database grow in size number of table rows parsed data stored into. example, if parsed data took 3 rows store, each subsequent application launch increase database size of (in terms of rows) six, nine, twelve, , on , forth.

what prevent table increasing in size upon each application launch. have tried resetting database each time, i'm not sure how correctly go or if solution should going for.

i have view display database in entirety:

public class qaview extends activity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.qa_view);          textview tv = (textview) findviewbyid(r.id.tvdata);         qadatabase entries = new qadatabase(this);         try         {             entries.open();             string data = entries.getalldata();             tv.settext(data);             entries.close();         }         catch(sqlexception e)         {             e.printstacktrace();         }     } } 

i have parser code processes data .xml file:

    xmlresourceparser parser = getresources().getxml(r.xml.qa);      try     {         while(parser.next() != xmlpullparser.end_document)         {             if(parser.geteventtype() != xmlpullparser.start_tag)             {                 continue;             }              string name = parser.getname();              if(name.equals("question"))             {                 question = readtextquestion(parser);             }             else if(name.equals("options"))             {                 readtextanswers(parser);             }         }     }     catch(xmlpullparserexception e)     {         e.printstacktrace();     }     catch(ioexception e)     {         e.printstacktrace();     } 

i have corresponding database code:

public class qadatabase {     public static final string key_rowid = "_id";     public static final string key_question = "question";     public static final string key_answer1 = "answer1";     public static final string key_answer2 = "answer2";     public static final string key_answer3 = "answer3";     public static final string key_answer4 = "answer4";     public static final string key_answer5 = "answer5";     public static final string key_correct = "correct";      public static final string database_name = "questionanswerdatabase";     public static final string database_table = "questionanswertable";     public static final int database_version = 2;      private databasehelper ourhelper;     private final context ourcontext;     private sqlitedatabase ourdatabase;      private static class databasehelper extends sqliteopenhelper     {         private databasehelper(context context)         {             super(context, database_name, null, database_version);         }          @override         public void oncreate(sqlitedatabase sqlitedatabase)         {             sqlitedatabase.execsql("create table " + database_table + " (" +                     key_rowid + " integer primary key, " +                     key_question + " text not null, " +                     key_answer1 + " text not null, " +                     key_answer2 + " text not null, " +                     key_answer3 + " text not null, " +                     key_answer4 + " text not null, " +                     key_answer5 + " text not null, " +                     key_correct + " text not null);"             );         }          @override         public void onupgrade(sqlitedatabase sqlitedatabase, int i, int i2)         {             sqlitedatabase.execsql("drop table if exists " + database_table);             oncreate(sqlitedatabase);         }     }      public qadatabase(context c)     {         ourcontext = c;     }      public qadatabase open() throws sqlexception     {         ourhelper = new databasehelper(ourcontext);         ourdatabase = ourhelper.getwritabledatabase();         return this;     }      public void close()     {         ourhelper.close();     }      public void createentry(string question, string answer1, string answer2, string answer3, string answer4, string answer5, string correct)     {         contentvalues cv = new contentvalues();         cv.put(key_question, question);         cv.put(key_answer1, answer1);         cv.put(key_answer2, answer2);         cv.put(key_answer3, answer3);         cv.put(key_answer4, answer4);         cv.put(key_answer5, answer5);         cv.put(key_correct, correct);         ourdatabase.insert(database_table, null, cv);     }      public string getalldata()     {         string[] columns = new string[] {key_rowid, key_question, key_answer1, key_answer2, key_answer3, key_answer4, key_answer5, key_correct};         cursor c = ourdatabase.query(database_table, columns, null, null, null, null, null);         string result = "";          while(c.movetonext())         {             result = result + c.getstring(0) + " " + c.getstring(1) + " " + c.getstring(2) + " " + c.getstring(3) + " " + c.getstring(4) + " " + c.getstring(5) + " " + c.getstring(6) + " " + c.getstring(7) + "\n";         }          return result;     }      public string getquestion(long l) throws sqlexception     {         string[] columns = new string[] {key_rowid, key_question, key_answer1, key_answer2, key_answer3, key_answer4, key_answer5, key_correct};         cursor c = ourdatabase.query(database_table, columns, key_rowid + "=" + l, null, null, null, null);          if(c != null)         {             // move cursor first row             c.movetofirst();              // id "l" in "questions" column             string question = c.getstring(1);              return question;         }          // otherwise, return null         return null;     }      public string getanswerchoice(long l, int answerchoicenumber) throws sqlexception     {         string[] columns = new string[] {key_rowid, key_question, key_answer1, key_answer2, key_answer3, key_answer4, key_answer5, key_correct};         cursor c = ourdatabase.query(database_table, columns, key_rowid + "=" + l, null, null, null, null);          if(c != null)         {             // move cursor first row             c.movetofirst();              // id "l" in "answers" column             string correctanswer = c.getstring(answerchoicenumber + 2); // need offset 2 key_rowid , key_question columns              return correctanswer;         }          // otherwise, return null         return null;     }      public cursor getalltitles()     {         return ourdatabase.query(database_table, new string[] {                 key_rowid, key_answer1, key_answer2, key_answer3, key_answer4, key_answer5, key_correct},                 null, null, null, null, null);     } 

you can assign fixed primary id (rowid) each question, doing again , again gives duplicate primary key constraint exception, table wouldn't grow.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -