title: Python MS Access Database Table Creation From Pandas Dataframe Using SQLAlchemy
body: <p>I'm trying to create an MS Access database from Python and was wondering if it's possible to create a table directly from a pandas dataframe.  I know that I can use pandas <code>dataframe.to_sql()</code> function to successfully write the dataframe to an SQLite database or by an using sqlalchemy engine for some other database format (but not Access unfortunately) but I can't get all the pieces parts to come together. Here's the code snippet that I've been testing with:</p>
  
  <pre><code>import pandas as pd
  import sqlalchemy
  import pypyodbc     # Used to actually create the .mdb file
  import pyodbc
  
  # Connection function to use for sqlalchemy
  def Connection():
      MDB = 'C:\\database.mdb'
      DRV = '{Microsoft Access Driver (*.mdb)}'
      connection_string = 'Driver={Microsoft Access Driver (*.mdb)};DBQ=%s' % MDB
      return pyodbc.connect('DRIVER={};DBQ={}'.format(DRV,MDB))
  
  
  # Try to connect to the database
  try:
      Conn = Connection()
  # If it fails because its not been created yet, create it and connect to it
  except:
      pypyodbc.win_create_mdb(MDB)
      Conn = Connection()
  
  # Create the sqlalchemy engine using the pyodbc connection
  Engine = sqlalchemy.create_engine('mysql+pyodbc://', creator=Connection)
  
  # Some dataframe
  data = {'Values'     : [1., 2., 3., 4.],
          'FruitsAndPets'  : ["Apples", "Oranges", "Puppies", "Ducks"]}
  df = pd.DataFrame(data)
  
  # Try to send it to the access database (and fail)
  df.to_sql('FruitsAndPets', Engine, index = False)
  </code></pre>
  
  <p>I'm not sure that what I'm trying to do is even possible with the current packages I'm using but I wanted to check here before I write my own hacky dataframe to MS Access table function.  Maybe my sqlalchemy engine is set up wrong? </p>
  
  <p>Here's the end of my error with <code>mssql+pyodbc</code> in the engine:</p>
  
  <pre><code>cursor.execute(statement, parameters)
  sqlalchemy.exc.DBAPIError: (Error) ('HY000', "[HY000] [Microsoft][ODBC Microsoft Access Driver] Could not find file 'C:\\INFORMATION_SCHEMA.mdb'. (-1811) (SQLExecDirectW)") u'SELECT [COLUMNS_1].[TABLE_SCHEMA], [COLUMNS_1].[TABLE_NAME], [COLUMNS_1].[COLUMN_NAME], [COLUMNS_1].[IS_NULLABLE], [COLUMNS_1].[DATA_TYPE], [COLUMNS_1].[ORDINAL_POSITION], [COLUMNS_1].[CHARACTER_MAXIMUM_LENGTH], [COLUMNS_1].[NUMERIC_PRECISION], [COLUMNS_1].[NUMERIC_SCALE], [COLUMNS_1].[COLUMN_DEFAULT], [COLUMNS_1].[COLLATION_NAME] \nFROM [INFORMATION_SCHEMA].[COLUMNS] AS [COLUMNS_1] \nWHERE [COLUMNS_1].[TABLE_NAME] = ? AND [COLUMNS_1].[TABLE_SCHEMA] = ?' (u'FruitsAndPets', u'dbo')
  </code></pre>
  
  <p>and the ending error for <code>mysql+pyodbc</code> in the engine:</p>
  
  <pre><code>cursor.execute(statement, parameters)
  sqlalchemy.exc.ProgrammingError: (ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Microsoft Access Driver] Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'. (-3500) (SQLExecDirectW)") "SHOW VARIABLES LIKE 'character_set%%'" ()
  </code></pre>
  
  <p>Just to note, I don't care if I use sqlalchemy or pandas <code>to_sql()</code> I just am looking for some easy way of getting a dataframe into my MS Access database easily.  If that's dump to JSON then a loop function to insert rows using SQL manually, whatever, if it works well I'll take it.</p>