2

In my activity I have 2 Datepicker with which I choose the starting date (for example 01/01/2014) and end date (for example 12/01/2014). then I have to insert 12 records at the same time with their dates, that is, in my date field there will be the following lines:

01/01/2014
01/02/2014
01/03/2014 
01/04/2014 
01/05/2014
01/06/2014
01/07/2014 
01/08/2014
01/09/2014
01/10/2014
01/11/2014 
01/12/2014 

until now have only been able to calculate the difference between the starting date and the ending date, but I do not know exactly how to put all those records together. Thanks for your help.

my code to calculate the difference between dates (JodaTime)

public void diff_date(View v){
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
    String date_in = sdf.format( dateAndTime.getTime() );
    String date_out = sdf.format( dateAndTime1.getTime() );
     int differenza_date = Days.daysBetween(new DateTime(date_in), new DateTime(date_out)).getDays(); 
user3160725
  • 1,801
  • 3
  • 15
  • 14

1 Answers1

3

As in SQL you can insert more than row you can making your string like that

INSERT INTO 'tablename' ('column1') VALUES
('val1'),
('val2'),
('val2'),
('val2');

and here implementation of insertion code :

public void diff_date(View v){
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
    String insertRows = "INSERT INTO 'tablename' ('column1') VALUES";
     String date_in = sdf.format( dateAndTime .getTime() );
     String date_out = sdf.format( dateAndTime1.getTime() );
     int differenza_date = Days.daysBetween(new DateTime(date_in), new DateTime(date_out)).getDays();
     for(int i=1 ; i < differenza_date ; i++)
     {
         // increase i day each time and save it in string 
         insertRows += "("+increasedDate+"),"
     }

     // and here insert into database 
     db.execSQL(insertRows)
     }
mohammed momn
  • 3,230
  • 1
  • 20
  • 16
  • yes, but it should be dynamic, that is, the user will have full freedom of choice of start date and end date. In my question is 1 year old, but the user may choose 2 years or 3 years or 6 months and so on. – user3160725 Jan 24 '14 at 22:32
  • take look on edited answer i try to made some dummy code , cause i don't have your full code , so feel free if any line of code doesn't obvious for you – mohammed momn Jan 24 '14 at 22:54
  • 3
    FYI, this syntax was introduced in sqlite 3.7.11 and many Android devices ship with older versions of the library, causing syntax errors if SQL like this is attempted to run. It's safer to do inserts just one row at a time. – laalto Jan 25 '14 at 14:37