Setting workspace Q

2085
4
Jump to solution
02-12-2014 09:30 AM
TonyAlmeida
Occasional Contributor II
I have the following script in a tool it runs great in a personal gdb, but when i trying to change the work space to SQL Server Express databases i get the error RuntimeError: cannot open workspace. I have a script with the work space as r"Database Servers\DSD15_SQLEXPRESS.gds\Tony_Two_way" and it works fine. What am i getting the error?

Traceback (most recent call last):   File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 18, in onMouseDownMap     edit = arcpy.da.Editor(workspace) RuntimeError: cannot open workspace


Tool script i am working with
import arcpy import pythonaddins import os from arcpy import env  class Add_points(object):     """Implementation for AddPoints_addin.Add_points (Tool)"""     def __init__(self):         self.enabled = True         self.cursor = 3 # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.     def onMouseDownMap(self, x, y, button, shift):          fc = "TonyTwoWay.DBO.CCAP_TEST"         workspace = r"Database Servers\DSD15_SQLEXPRESS.gds\Tony_Two_way"         arcpy.env.overwriteOutput = True          # Start an edit session. Must provide the worksapce.         edit = arcpy.da.Editor(workspace)          # Edit session is started without an undo/redo stack for versioned data         #  (for second argument, use False for unversioned data)         edit.startEditing(True)          # Start an edit operation         edit.startOperation()          list = []         with arcpy.da.SearchCursor(fc, ["AddressID"]) as cursor:             for row in cursor:                 list.append(int(row[0].strip("CC")))                 del cursor          list.sort()         AddressID = list[-1] + 1         AddressID = 'CC' + str(AddressID)          row_values = [(x, y, (x, y), AddressID)]         cursor = arcpy.da.InsertCursor(fc, ["X_Coord", "Y_Coord", "SHAPE@XY", "ADDRESSID"])          for row in row_values:             cursor.insertRow(row)         del cursor          arcpy.RefreshActiveView()                  # Stop the edit operation.         edit.stopOperation()          # Stop the edit session and save the changes         edit.stopEditing(True)                 pass
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Right-click on your SQL Server Express database > Save Connection.  This will save it as an SDE connection file under "Database Connections".  Then set your workspace to this .sde connection file.  Ex:

workspace = r"Database Connections\<connection name>.sde"


Do you still receive the error?

View solution in original post

0 Kudos
4 Replies
TonyAlmeida
Occasional Contributor II
I am trying to apply this to a addon tool.
I have tried adding the complete path to the database like so
fc = "Tony_Two_way.DBO.TT"
workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\DSD15_SQLEXPRESS.gds\TonyTwoWay"
I still the the same error:
Traceback (most recent call last):
  File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 18, in onMouseDownMap
    edit = arcpy.da.Editor(workspace)
RuntimeError: cannot open workspace


When i the code to a script in Arctoolbox it gets passed the RuntimeError: cannot open workspace but i get this error.

Traceback (most recent call last):
  File "C:\GIS\Python Scripts\AddPoints_updateXY_AddressID.py", line 30, in <module>
    row_values = [(x, y, (x, y), AddressID)]
NameError: name 'x' is not defined


Any help would be gratefully appreciated.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Right-click on your SQL Server Express database > Save Connection.  This will save it as an SDE connection file under "Database Connections".  Then set your workspace to this .sde connection file.  Ex:

workspace = r"Database Connections\<connection name>.sde"


Do you still receive the error?
0 Kudos
TonyAlmeida
Occasional Contributor II
JSkinns i have done what you said and i set the workspace to
workspace = r"Database Servers\dsd15_sqlexpress.sde"

I am still getting the same error.
Traceback (most recent call last):
  File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 18, in onMouseDownMap
    edit = arcpy.da.Editor(workspace)
RuntimeError: cannot open workspace


If i set the edit = arcpy.da.Editor(workspace) to edit = arcpy.da.Editor(env.workspace)

I get past the "cannot open Workspace" error
but i get the following error now.

Traceback (most recent call last):
  File "C:\Users\talmeida\AppData\Local\ESRI\Desktop10.1\AssemblyCache\{BB229966-100D-4482-A3FE-B298BD262597}\AddPoints_addin.py", line 32, in onMouseDownMap
    list.append(int(row[0].strip("CC")))
ValueError: invalid literal for int() with base 10: 'IDCCD08_FIRE'


Why does it work with edit = arcpy.da.Editor(env.workspace)
an not edit = arcpy.da.Editor(workspace)?

I do have an attribute 'IDCCD08_FIRE' in one of the points.
How do i by pass this attribute and and others. I am only interested in attributes with just CC. Can i just sorting attributes with just CC and calculating the highest number with just CC?
0 Kudos
TonyAlmeida
Occasional Contributor II
JSkinns i got it to work with the following..

so in ArcCatalog i right-clicked on the newly created connection from the instructions above, selected properties and in the Gerneral tab, Name: there is the complete path to the Connection file.

I used this in my workspace and it worked!
working code.
fc = "TonyTwoWay.DBO.TT"
workspace = r"C:\Users\talmeida\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\Connection to dsd15_sqlexpress.sde"

# Start an edit session. Must provide the worksapce.
edit = arcpy.da.Editor(workspace)


Thanks again for your help!