Άνοιγμα και επεξεργασία βάσεων δεδομένων της MS-Access τύπου mdb (πιθανά να παίζει και για *.accdb αλλά δεν το έχω δοκιμάσει).
dependencies :
pyodbc. Για την ώρα μόνο Python 2 αλλά σύμφωνα με το wiki του pyodbc ή έκδοση για Python 3 ετοιμάζεται
Σε linux πρέπει να έχει σεταριστεί και το ODBC μέσω του
UnixODBC ή του
iODBC. Σε windows οι driver είναι ήδη περασμένοι αν είναι εγκατεστημένοι η MS Access. Για να το ελέγξετε δώστε από γραμμή εντολών odbcad32.exe και δείτε την καρτέλα Drivers.
Αν οι drivers, δεν είναι εγκατεστημένοι (δηλαδή δεν έχει εγκατασταθεί η Access), νομίζω ότι είναι αυτοί :
www.microsoft.com/download/en/details.aspx?id=7151
from __future__ import unicode_literals
import os
import os.path
import pyodbc
class AccessDB(object):
def __init__(self, DB_name):
super(AccessDB, self).__init__()
# Form the path to the *.mdb file
cwd = os.path.abspath(os.path.curdir)
self.DB_name = DB_name
self.DB_path = os.path.join(cwd, DB_name)
self._get_drivers()
self.connectDB()
def _get_drivers(self, key = "MS Access Database"):
self.drivers = pyodbc.dataSources()
self.DB_driver = self.drivers[key]
def connectDB(self, auto = False):
conn_string = "DRIVER={" + self.DB_driver + '};DBQ=' + self.DB_path
self.conn = pyodbc.connect(conn_string, autocommit = auto)
self.cursor = self.conn.cursor()
def closeDB(self):
self.cursor.close()
self.conn.close()
def get_table_names(self):
self.tables = []
for row in self.cursor.tables():
self.tables.append(row.table_name)
def select_everything(self, tname):
selection = []
SQL = """
SELECT * FROM [{0}];
""".format(tname)
for row in self.cursor.execute(SQL):
selection.append(row)
return selection
def create_table(self, tname):
""" Creates an empty table. """
if self.cursor.tables(table=tname).fetchone():
print("TABLE {0} already exists.".format(tname))
else:
SQL = """CREATE TABLE [{0}];""".format(tname)
self.cursor.execute(SQL)
print("Created TABLE {0}.".format(tname))
def drop_table(self, tname):
if not self.cursor.tables(table=tname).fetchone():
print("Cannot drop TABLE {0}. It doesn't exist.".format(tname))
else:
SQL = """DROP TABLE [{0}]""".format(tname)
self.cursor.execute(SQL)
print("Dropped TABLE {0}.".format(tname))
def copy_table(self, source_name, target_name):
""" Copies an entire table. """
if self.cursor.tables(table=target_name).fetchone():
print("Table {0} already exists.".format(target_name))
else:
SQL = """
SELECT * INTO [{0}] FROM [{1}];
""".format(target_name, source_name)
self.cursor.execute(SQL)
print("Copied TABLE {0} INTO {1}".format(source_name,target_name))
def insert_column(self, tname, cname, dtype):
""" Inserts a column to a table."""
# check if the table exists
if not self.cursor.tables(table=tname).fetchone():
print("Cannot insert COLUMN {0}. TABLE {0} doesn't exist!".format(cname, tname))
# check if the column exists
elif self.cursor.columns(table = tname, column = cname).fetchone():
print("Cannot insert COLUMN {0}. COLUMN {0} already exists in TABLE {1}!".format(cname, tname))
else:
SQL = """
ALTER TABLE [{0}]
ADD COLUMN [{1}] {2};
""".format(tname, cname, dtype)
self.cursor.execute(SQL)
def main():
fname = "test.mdb"
db = AccessDB(fname)
db.get_table_names()
print db.tables
if __name__ == "__main__":
main()