diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh
index de7704d..f4e65a8 100755
--- a/core/scripts/run-tests.sh
+++ b/core/scripts/run-tests.sh
@@ -16,6 +16,7 @@
 const SIMPLETEST_SCRIPT_COLOR_PASS = 32; // Green.
 const SIMPLETEST_SCRIPT_COLOR_FAIL = 31; // Red.
 const SIMPLETEST_SCRIPT_COLOR_EXCEPTION = 33; // Brown.
+const SIMPLETEST_SCRIPT_SQLIGHT_VARIABLE_LIMIT = 999;
 
 // Set defaults and get overrides.
 list($args, $count) = simpletest_script_parse_args();
@@ -907,10 +908,22 @@ function simpletest_script_reporter_display_summary($class, $results) {
 function simpletest_script_reporter_write_xml_results() {
   global $args, $test_ids, $results_map;
 
-  $results = Database::getConnection('default', 'test-runner')
-    ->query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(
-      ':test_ids' => $test_ids,
-    ));
+  // Load the simpletest results from the database. Load it in chunks if sqlight
+  // requires this.
+  if (count($test_ids) > SIMPLETEST_SCRIPT_SQLIGHT_VARIABLE_LIMIT && !empty($args['sqlite'])) {
+    $test_id_chunks = array_chunk($test_ids, SIMPLETEST_SCRIPT_SQLIGHT_VARIABLE_LIMIT);
+  }
+  else {
+    $test_id_chunks = array($test_ids);
+  }
+  $results = array();
+  foreach ($test_id_chunks as $test_id_chunk) {
+    $result = Database::getConnection('default', 'test-runner')
+      ->query("SELECT * FROM {simpletest} WHERE test_id IN (:test_ids) ORDER BY test_class, message_id", array(
+        ':test_ids' => $test_id_chunk,
+      ));
+    $results = array_merge($results, $result);
+  }
 
   $test_class = '';
   $xml_files = array();
