Android的SQLite的插入多条记录 [英] Android SQLite insert multiple records

查看:258
本文介绍了Android的SQLite的插入多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我有2日期选择器与我选择的出发日期(例如2014年1月1日)和结束日期(例如2014年12月1号)。然后,我要插入12条记录,同时与他们的日期,那就是,在我的日期字段会有以下行:

  2014年1月1日
2014年1月2日
2014年1月3日
2014年1月4日
2014年1月5日
2014年1月6日
2014年1月7日
2014年1月8日
2014年1月9日
2014年1月10日
2014年1月11日
2014年1月12日
 

到现在为止只能够计算起始日期和结束日期之间的差别,但我不知道究竟如何将所有这些记录放在一起。感谢您的帮助。

我的code,计算日期之间的差值(JodaTime)

 公共无效diff_date(视图v){
    SimpleDateFormat的SDF =新的SimpleDateFormat(YYYY-MM-DD);
    串date_in = sdf.format(dateAndTime.getTime());
    串date_out = sdf.format(dateAndTime1.getTime());
     。INT differenza_date = Days.daysBetween(新的日期时间(date_in),新的日期时间(date_out))getDays();
 

解决方案

作为SQL可以插入多行,你可以让你的字符串一样,

  INSERT INTO'表名'('列1')VALUES
('val1中'),
('val2中),
('val2中),
('将val2');
 

在这里执行插入code:

 公共无效diff_date(视图v){
    SimpleDateFormat的SDF =新的SimpleDateFormat(YYYY-MM-DD);
    字符串insertRows =INSERT INTO'表名'('列1')VALUES;
     串date_in = sdf.format(DateAndTime的.getTime());
     串date_out = sdf.format(dateAndTime1.getTime());
     。INT differenza_date = Days.daysBetween(新的日期时间(date_in),新的日期时间(date_out))getDays();
     的for(int i = 1; I< differenza_date;我++)
     {
         //增加一天我每次并将其保存在弦
         insertRows + =(+ increasedDate +),
     }

     //并插入到数据库
     db.execSQL(insertRows)
     }
 

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(); 

解决方案

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)
     }

这篇关于Android的SQLite的插入多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆