title: A long IN clause vs a lot of queries in SQLite on Android
body: <p>I have got a table of blacklisted Bluetooth devices and need to make sure that my device is paired with none of them. The blacklisted devices are stored in a database accessed through a content provider.</p>
  
  <p>Which of the approaches is better?</p>
  
  <ul>
  <li>I build an <code>IN</code> clause and then query data once. The <code>IN</code> clause might contain very many values. For example.</li>
  </ul>
  
  <p><code>SELECT _id FROM blacklisted_devices WHERE mac_address IN ('01:23:45:67:89:ab', ' ab:23:45:67:89:df') OR device_name IN ('Lorem', 'Ipsum')</code></p>
  
  <ul>
  <li><p>I use a for loop and query data in it.</p>
  
  <pre><code>Set&lt;BluetoothDevice&gt; bondedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
  Cursor cursor = null;
  for (BluetoothDevice device : bondedDevices) {
      cursor = getContentResolver().query(Uris.BLUETOOTH,
              new String[] { Columns._ID },
              Columns.DEVICE_NAME + "=? OR " + Columns.MAC_ADDRESS + "=?",
              new String[] { device.getName(), device.getAddress() },
              null);
      if (cursor.getCount() &gt; 0) {
          // Unpair
      }
  }
  </code></pre></li>
  </ul>