debugged
debugged

Reputation: 347

Add new field to access table using python

I have an access table that I am trying to add fields programmatically using Python. It is not a personal geodatabase. Just a standard Access database with some tables in it.

I have been able to access the table and get the list of field names and data types.

How do I add a new field and assign the data type to this Access table using Python.

Thanks! SRP

Upvotes: 0

Views: 2519

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169524

Using the pyodbc module:

import pyodbc

MDB = 'c:/path/to/my.mdb'
DRV = '{Microsoft Access Driver (*.mdb)}'
PWD = 'my_password'

conn = pyodbc.connect('DRIVER=%s;DBQ=%s;PWD=%s' % (DRV,MDB,PWD))
c = conn.cursor()
c.execute("ALTER TABLE my_table ADD COLUMN my_column INTEGER;")
conn.commit()
c.close()
conn.close()

Edit:
Using win32com.client...

import win32com.client
conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=c:/path/to/my.mdb;'
conn.Open(DSN)
conn.Execute("ALTER TABLE my_table ADD COLUMN my_column INTEGER;")
conn.Close()

Upvotes: 1

Related Questions