Forum Discussion

talisker's avatar
talisker
New Contributor
7 years ago
Solved

Retrieving the column names from the result of an ADO database query in Python

Hi all, 

 

I want to create a somewhat generic function in Python where I get a key/value dictionary from a database query that I know returns only one row. I want to do something like this:

 

 

def GetEmployeeInfo(employeeId): 
  Conn = ADO.CreateADOCommand();
  Conn.ConnectionString = "Provider=OraOLEDB.Oracle.1;Persist Security Info=False;User ID=********;Password=********;Data Source=********:****"
 
  # I get ALL the columns from the table
  Conn.CommandText =  "SELECT * FROM Employee WHERE EmployeeId = :paramEmployeeId"
  Conn.CommandType = cmdText;
  Conn.Parameters.Items[0].Value = employeeId;
  
  data = Conn.Execute();
  data.MoveFirst();

  result = {}
  
  # Here, I would like to build a dictionnary like: 
# result[COLUMN_NAME] = data.Fields.Item[index].Value
# but I have no idea how to retrieve the column name
# and cannot find any detailed documentation of methods and
# properties around data or data.Fields, etc... for index in range(data.Fields.Count): Log.Message(data.Fields.Item[index].Value) return result

 

Now, I am able to get all the cell values from the returned database row, but I need something that would get me this sort of result:

 

 

result = GetEmployeeInfo(99)

Log.Message(result["EmployeeId"])
Log.Message(result["FirstName"])
Log.Message(result["LastName"])
(...)

 

I am sure this is doable, but I find the API documentation quite lacking when it comes to finding out about all available methods or properties of objects coming from ADO commands.

 

Thanks in advance for your help,

 

Regards

  • data.Fields.Item[index].Name

    The above will return the name of the item at the indicated index. 

1 Reply

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    data.Fields.Item[index].Name

    The above will return the name of the item at the indicated index.