title: How to programmatically convert multiple db files to csv in python or R?
body: <p>I have thousands of <code>*.db</code> files containing sensor data. All <code>*.db</code> files have the same structure like, <code>ID,MACID,RSSI,ENTRY,EXIT,DEVICEID,UNIQUEID</code>. I have seen a similar question like <a href="https://stackoverflow.com/questions/37375344/convert-multiple-dbs-to-csv">this one</a> but the solution does not work for me. </p>
  
  <p>What I have tried so far:</p>
  
  <ol>
  <li><p>Initially tried <code>DB Browser for SQLite</code> to individually convert each <code>*.db</code>  to <code>.csv</code> format. It's a lot of copy-paste and I hated it!</p></li>
  <li><p>Using online forums, created a makeshift script as follows;</p></li>
  </ol>
  
  <pre><code>import sqlite3
  from sqlite3 import Error
  
  import pandas as pd
  dataPath = "path_to_a_single_db_file"
  # create database connection
  conn = sqlite3.connect(dataPath)
  # a custom function to create database connection
  # Reference: http://www.sqlitetutorial.net/sqlite-python/sqlite-python-select/
  def create_connection(db_file):
                try:
                     conn = sqlite3.connect(db_file)
                      return conn
                except Error as e:
                      print(e)
  
                return None
  # create a function for reading &amp; writing the Wifi table
  
  def read_write_wifi_tbl_to_csv(conn):
              cur = conn.cursor()
              query='select * from WIFI'
              data=pd.read_sql(query,conn)
              data.to_csv('export.csv')
  
  def main():
      # create a database connection
      conn = create_connection(dataPath)
      with conn:
        print("1. Query and writing to Wifi table")
        read_write_wifi_tbl_to_csv(conn)
  
  if __name__ == '__main__':
                  main()
  </code></pre>
  
  <p>The above code snippet works well for a single db file. How to make this work for multiple db files. </p>