Written by

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?

Comments

Eduard Lebedyuk · Nov 13, 2020

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).

0
Scott Fadden · Nov 13, 2020

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.
0
Eduard Lebedyuk  Nov 13, 2020 to Scott Fadden

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.

0
Mark Morris · Jun 17, 2025
Well, it's been several years since this was asked, but I also needed the answer, 
and I found a way to create a database using Python.  
I verified that the new database has been created in the SMP.  
I didn't set the directory, but it put the new database in /data/iris/sys/mgr/, 
where `iris` is my instance name. 
The key step to my progress was to get the IRISReference and pass it, 
even though it is empty.

presuming you already have made the connection...
irispy = iris.createIRIS(conn)

Name = "ABC"
db_exists = irispy.classMethodValue('Config.Databases', 'Exists', Name)
if not db_exists:
   ref_object = iris.IRISReference(None);
   status_db = irispy.classMethodValue('Config.Databases', 'Create', Name, ref_object)
   if status_db != 1:
      print(f"Database create failed: {status_db}")
0