2013-04-05 125 views
1

我有以下功能,它可以從一個數據庫中讀取大約15,000行數據,格式化數據並寫入另一個數據庫。目前這需要大約6秒鐘的時間來執行。有沒有辦法做到這一點,而不使用JavaScript來格式化數據,並只使用SQLite查詢?或者另一種方法來加速它?JS SQLITE,讀取一個數據庫並寫入另一個數據庫

感謝

 var events = db.open(events.sqlite3); 
     function updateTimetable() { 
     var rows = users.execute("SELECT id, complete, strftime('%s000',time) AS realTime, strftime('%H:%M',time) AS Time FROM exerciseLists WHERE patientId = ? ORDER BY time ASC", Alloy.Globals.currentUser); 
      events.execute("DELETE FROM Events"); 
      events.execute('BEGIN'); 
      while (rows.isValidRow()) { 
       var colorType; 
       var active; 
       var time = rows.fieldByName('Time'); 
       var realTime = rows.fieldByName('realTime'); 
       var now = new Date().getTime(); 
       if (realTime < now) { 
        colorType = 'red'; 
        active = 'no'; 
       } else { 
        colorType = 'blue'; 
        active = 'no'; 
       }; 
       if (rows.fieldByName('complete')) { 
        colorType = 'yellow'; 
       } 
       if (Alloy.Globals.sessionButton.button.sessionId === rows.fieldByName('id')) { 
        colorType = 'green'; 
        active = 'yes'; 
       } 
       function pad(n) { 
        return n < 10 ? '0' + n : n 
       } 

       var d = new Date(parseInt(realTime)); 
       var dYear = d.getFullYear(); 
       var dDay = pad(d.getDate()); 
       var dMonth = pad(d.getMonth()+1); 
       var dHour = pad(d.getHours()+1); 
       var dMinute = pad(d.getMinutes()+1); 

       var dFinal = dYear + "-" + dMonth + "-" + dDay + " " + dHour + ":" + dMinute; 

       events.execute("INSERT INTO Events (title, date_start, date_end, note, location, identifier, type, attendees, organizer) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", time, dFinal, '(null)', rows.fieldByName('id').toString(), colorType, rows.fieldByName('id').toString(), active, '(null)', '(null)'); 

       rows.next(); 
      } 
      rows.close(); 
      events.execute('COMMIT'); 
      Alloy.Globals.refreshTimetable(); 
     }; 

回答

1

Attach一個數據庫到另一個,然後使用INSERT ... SELECT ...

ATTACH 'users.sqlite' AS users_db; 

INSERT INTO Events (title, date_start, date_end, 
        note, location, identifier, 
        type, attendees, organizer) 
SELECT strftime('%H:%M', time), 
     strftime('%Y-%m-%d %H:%M', time), 
     '(null)', -- why not NULL? 
     id, 
     CASE WHEN id = ? /* sessionId */ THEN 'green' 
      WHEN complete     THEN 'yellow' 
      WHEN time < CURRENT_TIMESTAMP THEN 'red' 
      ELSE        'blue' 
     END, 
     id, 
     CASE WHEN id = ? /* sessionId */ THEN 'yes' 
      ELSE        'no' 
     END, 
     '(null)', 
     '(null)' 
FROM users_db.exerciseLists 
WHERE patientId = ? 
ORDER BY time; 
+0

驚人。謝謝。 (null)是因爲第三方函數已經在讀取數據庫。 – 2013-04-06 21:35:32

相關問題