2

Does anyone have any examples of creating a new Access Database and importing a CSV file (only specific fields) into the database?

Thanks

MapMan
  • 630
  • 2
  • 13
  • 28
  • There's a post that does the opposite (read mdb and write it to CSV), but hopefully it can get you started: http://stackoverflow.com/questions/3620539/how-to-deal-with-mdb-access-files-with-python – plaes Dec 20 '11 at 14:49

2 Answers2

2

Here is an idea and a link for further info:

I have not tested the following for creating a new mdb so ymmv!

import win32com.client
eng=win32com.client.gencache.EnsureDispatch("DAO.DBEngine.36")
eng.CreateDatabase("c:\\myNewAccessdB.mdb", win32com.client.constants.dbLangGeneral)

Here is a link to some good info for working with python and ado.

I hope this helps.

~M

Sorry I do not have any examples for working the csv into the empty mdb :( If I come with anything I will post later.

MWR
  • 170
  • 8
1

You can use PyPyODBC to do this.

To create an Access mdb file:

import pypyodbc
pypyodbc.win_create_mdb( "D:\\Your MDB file path.mdb" )

If you want, you can continue to use pypyodbc to connect to the creatmdb files and manipulate them with ODBC interface similar to pyodbc:

conn = pypyodbc.connect(u'''Driver={Microsoft Access Driver (*.mdb)};DBQ='''+mdb_path
                    , unicode_results = True
                    , readonly = False)

cur = conn.cursor()
cur.execute ('Drop table pypyodbc_test_tabl')
cur.execdirect(u"""create table pypyodbc_test_tabl (ID integer PRIMARY KEY,product_name text)""")

...
cur.close()
conn.commit()
conn.close()

Finally, To compact an existing Access mdb file

pypyodbc.win_compact_mdb("D:\\The path to the original to be compacted mdb file"
                   ,"D:\\The path to put the compacted new mdb file")
pypyodbc
  • 1,119
  • 1
  • 12
  • 9