Select to view content in your preferred language

Arcpy Table to Table Conversion of multiple tables

1608
4
Jump to solution
02-21-2022 01:24 PM
ColeNelson
Emerging Contributor

Hello everyone, I'm working on creating a tool that will allow me to select multiple tables from an Access Database and convert them to standalone tables in a Geodatabase:

Tools.PNG

I created a .odc database connection to the Access database the tables are being stored in hopes to make it easier to select the tables.  Here is the script that I have so far:

import arcpy
import os

#Allow code to overwrite existing datasets
arcpy.env.overwriteOutput = True

#Set the variables
workspace =  r"C:\UpdateDevelopmentPython\ForDeveloping\isrb_business.odc"
tables = arcpy.GetParameterAsText(0).split(";")
outpath = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb"
outfile = os.path.join(workspace, "{}" .format(os.path.join(workspace) + str(["baseName"])))

#Create the standalone tables
TblResponseID = arcpy.TableToTable_conversion(tables,outpath,outfile)

 However whenever I run the the tool I keep getting this message.

import arcpy
import os

#Allow code to overwrite existing datasets
arcpy.env.overwriteOutput = True

#Set the variables
workspace =  r"C:\UpdateDevelopmentPython\ForDeveloping\isrb_business.odc"
tables = arcpy.GetParameterAsText(0).split(";")
outpath = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb"
outfile = os.path.join(workspace, "{}" .format(os.path.join(workspace) + str(["baseName"])))

#Create the standalone tables
TblResponseID = arcpy.TableToTable_conversion(tables,outpath,outfile)
 
I want the new standalone tables to keep the same name as the original Access tables, is there a way to do this?  Thanks for the help!

 

0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

The parameters for the TableToTable are not right. It is expecting an input table, the output path, and just the output table name.  You have a full path (and string of a list "baseName") going into the table name parameter. 

Set your code up in a loop to iterate over the list of tables you get and you can grab the name of each table for the output.  Like Don mentioned, add some print statements or set some breakpoints and debug so you can see what your variables look like.

tables = arcpy.GetParameterAsText(0).split(';')

outpath = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb"

for tble in tables:
    baseName = os.path.basename(table)
    #Create the standalone tables
    TblResponseID = arcpy.TableToTable_conversion(tble, outpath, baseName)

 

View solution in original post

4 Replies
DonMorrison1
Frequent Contributor

I actually don't see the error message - I do see the source code in red - is that what you are calling the error message?

Also:

  1. Here are the file types supported by TabletoTable_conversion - I've never worked with Access files so maybe they work.:
    1. Geodatabase
    2. dBASE (.dbf)
    3. Comma-separated values (.csv or .txt)
    4. Microsoft Excel worksheets (.xls or .xlsx)
    5. In-memory table views
  2. I think you are going to have to copy one file at a time, it looks like you are trying to pass in a list
  3. I would also print out (if running in a toolbox, use AddMessage) the variables along the way as you assign them just to verify they are what you expect - once you have it working you can remove those
0 Kudos
ColeNelson
Emerging Contributor

Hi Don I copied and pasted the wrong thing lol

Traceback (most recent call last):
File "C:\UpdateDevelopmentPython\DevelopmentScripts\PublishFireStationWithType.py", line 13, in <module>
Tbls = arcpy.TableToTable_conversion(tables,outpath,["baseName"])
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 3112, in TableToTable
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\conversion.py", line 3109, in TableToTable
retval = convertArcObjectToPythonObject(gp.TableToTable_conversion(*gp_fixargs((in_rows, out_path, out_name, where_clause, field_mapping, config_keyword), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: Error in executing tool

0 Kudos
by Anonymous User
Not applicable

The parameters for the TableToTable are not right. It is expecting an input table, the output path, and just the output table name.  You have a full path (and string of a list "baseName") going into the table name parameter. 

Set your code up in a loop to iterate over the list of tables you get and you can grab the name of each table for the output.  Like Don mentioned, add some print statements or set some breakpoints and debug so you can see what your variables look like.

tables = arcpy.GetParameterAsText(0).split(';')

outpath = r"C:\UpdateDevelopmentPython\ForDeveloping\ForDeveloping.gdb"

for tble in tables:
    baseName = os.path.basename(table)
    #Create the standalone tables
    TblResponseID = arcpy.TableToTable_conversion(tble, outpath, baseName)

 

ColeNelson
Emerging Contributor

Hi Jeff that worked great, thank yo so much!

0 Kudos