Question
Scott Fadden · Nov 11, 2020

Create Database using Python

I am trying to create a database using python. The example shows setting a Name string and a Properties object containing Directory=.

; Use class methods to create an instance
 %SYS>s Name="ABC"
 %SYS>s Properties("Directory")="c:\abc\"
 %SYS>s Status=##Class(Config.Databases).Create(Name,.Properties)
 %SYS>i '$$$ISOK(Status) w !,"Error="_$SYSTEM.Status.GetErrorText(Status)

How do I update and pass the Directory property using Python?

 

0
0 167
Discussion (3)2
Log in or sign up to continue

You can either

1. You can use object access from Python so this code can be invoked via Native API for Python:

Set name = "ABC"
Set dir = ##class(%File).SubDirectoryName(##class(%File).ManagerDirectory(), name, 1)
Set sc = ##class(%File).CreateDirectory(dir)
Write $SYSTEM.Status.GetErrorText(sc)

Set db=##Class(SYS.Database).%New()
Set db.Directory = dir
Set sc = db.%Save()
Write $SYSTEM.Status.GetErrorText(sc)

Set dbConfig=##Class(Config.Databases).%New()
Set dbConfig.Directory = dir
Set dbConfig.Name = name
Set sc = dbConfig.%Save()
Write $SYSTEM.Status.GetErrorText(sc)

2. Use SQL via xDBC connection (CREATE DATABASE).

Eduard,

Thanks for the reply.

The challenge is that I cannot find a way to use the Python interface to pass a properties object to ClassMethodValue, and I don't see another appropriate function.  
 

  • iris.classMethodValue() — calls a user defined ObjectScript method and gets the returned value.
  • iris.classMethodVoid() — calls a user defined ObjectScript method, ignoring any returned value.
  • iris.function() — calls a function of a user defined ObjectScript routine and gets the returned value.
  • iris.procedure() — calls a procedure of a user defined ObjectScript routine.

As I said use object access instead.

I have written a code sample which can be used with Native API for Python (although the first part with directory creation should probably just be called from PYTHON as is if you're on a same machine).

Locals are not supported by Native API for Python.