Arcpy error : Object: Could not open SDE workspace

8286
7
06-07-2016 07:40 AM
DEVAPP
by
New Contributor III

Hi at all,

i have develop a python script that execute a query to table into sde connection (DB Oracle).

This my code:

import arcpy
from arcpy import env

env.workspace = ("Database Connections/TEST.sde")
sdeConn = arcpy.ArcSDESQLExecute("Database Connections/TEST.sde")
value = 'V'


sql = "select * from TEST.CATEGORIES WHERE TIP_SER = " + value
print(sql)
try:
    sdeReturn = sdeConn.execute(sql)
    print('ok')
except Exception, ErrorDesc:
    print ErrorDesc
    sdeReturn = False

But when i run i recive this error: RuntimeError: Object: Could not open SDE workspace

The sde connection works fine into ArcMap and also i have try other simple connection by arcpy for print a list table and works fine.

So why this error?

Any help?

Thanks

Tags (2)
0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

Your code is quite a bit different than the examples in the help topic ArcSDESQLExecute—Help | ArcGIS for Desktop

I would at least put your attempt at a connection within the try-except block and check the parameters, some of them don't appear to be optional.  Script functionality within arcmap is not the same as that outside that environment, there is nothing assumed or available, and anything that needs to be specified must be.

0 Kudos
DEVAPP
by
New Contributor III

Hi Dan,

thanks for your response.

I have seen the example that you have attached and as Syntax require some parameters not optional, but the .sde contain itself all the parameter request:

server, port, istance, use and psw etc

Example if i set sde connection as workspace for a list table the script works so i suppose that the connection is open:

arcpy.env.workspace = ("Database Connections/TEST.sde")

tables = arcpy.ListTables()

for t in tables:

    print(t)

0 Kudos
DougBrowning
MVP Esteemed Contributor

We just use the sde file directly.  So just c:\temp\db.sde for example.  Just make a connection then copy and paste it to any directory.  We have a Connections directory with links to all our dbs.

DEVAPP
by
New Contributor III

Hi Doug,

i have pasted the sde connection in other folder and after i have tried the script in this way but the result is always the same

0 Kudos
KenHartling
Esri Contributor

I see two things as possibly being a problem.

1. value = 'V'

The value in the sql syntax will need to be in quotes so change this to:

value = "'V'" (to make more clear it's... "  '  V  '  ")

2. If TEST.CATEGORIES is a feature class with a shape column "select * from..." will not work. Change this to:

sql = "select fld1, fld2, fld3 from TEST.CATEGORIES WHERE TIP_SER = " + value

Let me know if changing these helps.

Thanks. Ken

DEVAPP
by
New Contributor III

Hi Ken,

the query with value = " 'V' " is ok.

Now the sdeConn = arcpy.ArcSDESQLExecute("Database Connections/TEST.sde") works fine also without the query.

I have copied and pasted my code into other ArcGIS enviromets and works fine.

I suppose that on my machine after istall of python 3 there is something to wrong.

Thanks to all

0 Kudos
IngridMans
Occasional Contributor II

I'm having the same issue for just trying to run one lame arcpy.da.ListDatabaseSequences()

It works on a normal file gdb, but I can't do it on our sde database. ArcGIS Pro 2.7