如何在Sqlite android中更新多个记录的不同值?

时间:2018-06-01 06:59:56

标签: android sql sqlite android-sqlite

我有一个table.i想要使用一个列更改值(时间)多次复制相同的记录。我可以使用以下查询复制记录。

insert into acsl_details(userID,aadhar_no 
,date ,time ,in_out 
,deptid ,emptype ,compy_code ,empmode ,name 
,upload_flag ,main_compcode ,utc_date ,utc_time ,time_zone ,latitude ,longitude )
select * from acsl_details;

现在我有一千个重复记录。现在我要更改一列是time.i需要更新所有千条记录的不同时间。我搜索但无法理解这些查询。任何帮助..

2 个答案:

答案 0 :(得分:1)

只需在select中列出您想要的值:

insert into acsl_details (userID,aadhar_no, date, time, in_out, 
                          deptid, emptype, compy_code, empmode, name 
                          upload_flag, main_compcode  utc_date, utc_time,
                          time_zone, latitude, longitude
                         )
    select userID, aadhar_no, date,
           <new time value goes here>,
           in_out, 
           deptid, emptype, compy_code, empmode, name 
           upload_flag, main_compcode  utc_date, utc_time,
           time_zone, latitude, longitude                         
    from acsl_details;

答案 1 :(得分:1)

我相信以下内容会按照您的要求进行,即以hh:mm:ss: -

格式随机生成时间更新1000个现有行的时间列
WITH RECURSIVE newdata(id_of_row,newtime) AS (
    SELECT rowid AS id_of_row , 
         time(strftime('%s', '2000-01-01 00:00:00') +
             abs(random() % (strftime('%s', '2000-01-31 23:59:59') -
                                strftime('%s', '2000-01-01 00:00:00'))
                   ),
                'unixepoch') AS dt
    FROM acsl_details
)
UPDATE acsl_details 
    SET time = (
        SELECT newdata.newtime 
            FROM newdata 
            WHERE newdata.id_of_row = acsl_details.rowid
    );

核心代码/ SQL是: -

SELECT rowid AS id_of_row , 
 time(strftime('%s', '2000-01-01 00:00:00') +
            abs(random() % (strftime('%s', '2000-01-31 23:59:59') -
                            strftime('%s', '2000-01-01 00:00:00'))
               ),
            'unixepoch') AS dt
FROM acsl_details

这会从表中提取rowid,添加随机生成的时间,从而为acsl_details表的每一行生成两列( id_of_row dt )。

然后 newdata 驱动更新。正在对 newdata 中存在的每一行进行更新。

测试

以下用于创建100个相同的行: -

DROP TABLE IF EXISTS acsl_details;
CREATE TABLE IF NOT EXISTS acsl_details (userID integer, aadhar_no INTEGER, date INTEGER, time INTEGER, in_out INTEGER, deptid INTEGER, compy_code INTEGER, empmode INTEGER, name TEXT, upload_flag INTEGER, main_compcode TEXT, utc_date INTEGER, utc_time, time_zone, latitude REAL, longitude REAL);

WITH RECURSIVE counter(userid,aadhar,date,time,in_out,deptid,compy_code,empmode,name,upload_flag,main_compcode,utc_date,utc_time,time_zone,latitude,longitude) AS (
    SELECT 1,11,'2018-01-01','10:20:00',0,111,56078,25,'FRED',4,'PARTIAL',5000,1324,'Z',123.56,56.234 
    UNION ALL SELECT userid,aadhar,date,time,in_out,deptid,compy_code,empmode,name,upload_flag,main_compcode,utc_date,utc_time,time_zone,latitude,longitude FROM counter LIMIT 100)  
    INSERT INTO  acsl_details SELECT * FROM counter;

SELECT * FROM acsl_details;

e.g。 : -

enter image description here

按照解决方案运行代码后: -

enter image description here