I have an application where the input information is in a dBase III table. I need to copy this information to an existing table of the same schema in an Access database. The best method I have come up with is to open an ADO Connection and recordset to the dBase table and a second ADO connection and recordset to the output Access table. I truncate the Access table and then iterate thru the dBase recordset, use AddNew to add a new recordset to the Access table, and then copy the dBase field information into the new record. The input dBase table will always be around 70 to 80 thousand records. For 77,000 records the above method takes about 3 1/2 minutes. Is there a better way? The Python code I am been working with is below.
#
# Program: dBase_to_Access.py
# Purpose: Copy a dBase table to an existing empty Access table
# Using the Northwind db here just as an example.
#
import sys, os, string, traceback,time
from win32com.client import Dispatch
dBaseFolder = "C:\\MyDB"
dBaseTable = "EMPTAB.DBF"
AccessDatabase = "C:\\MyDB\\Northwind.mdb"
AccessTable = "Employees_Copy"
try:
# create an ADO connection object
inConn = Dispatch('ADODB.Connection
')
rsIn = Dispatch('ADODB.recordset'
)
outConn = Dispatch('ADODB.Connection
')
rsOut = Dispatch('ADODB.recordset'
)
inConnString = "Provider=Microsoft.Jet.OL
EDB.4.0;Da
ta Source=" + dBaseFolder + ";Extended Properties=dBASE IV;User ID=Admin;Password=;"
inConn.ConnectionString = inConnString
inConn.Open()
outConnString = "PROVIDER=Microsoft.Jet.OL
EDB.4.0;DA
TA SOURCE=" + AccessDatabase
outConn.ConnectionString = outConnString
outConn.Open()
#First truncate Access output table
outsql = "DELETE FROM " + AccessTable
outConn.Execute(outsql)
#Now open recordset for appending to Access table
outsql = "SELECT * FROM " + AccessTable
rsOut.Open(outsql,outConn,
1,3)
sFields = "LastName,FirstName"
insql = "SELECT " + sFields + " FROM " + dBaseTable
rsIn.Open(insql,inConn,0,1
)
rsIn.MoveFirst()
i = 0
while not rsIn.EOF:
i = i + 1
sFname = rsIn.Fields.Item("LastName
").Value
sLname = rsIn.Fields.Item('FirstNam
e').Value
rsOut.AddNew()
rsOut.Fields.Item("LastNam
e").Value = sFname
rsOut.Fields.Item("FirstNa
me").Value
= sLname
rsOut.Update()
rsIn.MoveNext()
print "\n %s records processed." % i
# close the database connection
inConn.Close()
outConn.Close()
inConn = None
rsIn = None
outConn = None
rsIn = None
print "Copy table complete.\n"
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
print pymsg
Start Free Trial