简体   繁体   English

在 python 3 中使用 subprocess.check_call 运行 Oracle DBCA

[英]Run Oracle DBCA with subprocess.check_call in python 3

I want to run Oracle DBCA to create a database in silent mode.我想运行 Oracle DBCA 在静默模式下创建数据库。 The command I want to run via Python script is the following one:我想通过 Python 脚本运行的命令如下:

dbca -silent -createDatabase -templateName General_Purpose.dbc -gdbName testdb -sid testdb -createAsContainerDatabase false -emConfiguration NONE -datafileDestination /u01/data/oracle/testdb/db_files -storageType FS -characterSet AL32UTF8 -totalMemory 2048 -recoveryAreaDestination /u01/app/oracle/fast_recovery_area

If I run that command directly from the bash console it runs perfectly.如果我直接从 bash 控制台运行该命令,它将完美运行。 Now I want to adapt it to be ran from a python script so I did the following:现在我想调整它以从 python 脚本运行,所以我做了以下事情:

subprocess.check_call(['dbca', '-silent', '-createDatabase', '-templateName', 'General_Purpose.dbc', '-gdbName', gdbName, '-sid', sid, '-createAsContainerDatabase', 'false', '-emConfiguration', 'NONE', '-datafileDestination', dfDest, '-storageType', storageType, '-characterSet', 'AL32UTF8', '-totalMemory', totalMemory, '-recoveryAreaDestination', recoAreaDest])

Not all of the arguments have single quotes and that's because I'm passing some variables as arguments.并非所有 arguments 都有单引号,这是因为我将一些变量作为 arguments 传递。 My variables are the following ones:我的变量如下:

#### VARIABLES ####
gdbName = ""
sid = ""
#dfDest = ""
storageType = "FS"
totalMemory = "2048"
recoAreaDest = oracle_base + "/fast_recovery_area"

#### USER INPUTS ####
while gdbName == "":
    gdbName = input("\n" + "Global DB name: ")
while sid == "":
    sid = input("SID: ")
dfDest = "/u01/data/oracle/" + sid + "/db_files"
dfDest = input("Datafile destination [" + dfDest + "]: ")
storageType = input("Storage type [" + storageType + "]: ")
totalMemory = input("Total Memory (in MB) [" + totalMemory + "]: ")
recoAreaDest = input("Fast recover area [" + recoAreaDest + "]: ")

So when I run my script the following error occurs:因此,当我运行我的脚本时,会发生以下错误:

[oracle@dataguard-test-primary ~]$ ./oracle_silent_install.py

1. Install Software
2. Create Database
     Option: 2


Oracle Database Creation script
Please enter the required info...


Global DB name: d
SID: d
Datafile destination [/u01/data/oracle/d/db_files]:
Storage type [FS]:
Total Memory (in MB) [2048]:
Fast recover area [/u01/app/oracle/fast_recovery_area]:
java.lang.NullPointerException
        at oracle.install.commons.swing.StandardDialog$ErrorDialogShowTask.call(StandardDialog.java:567)
        at oracle.install.commons.swing.StandardDialog$ErrorDialogShowTask.call(StandardDialog.java:516)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
[oracle@dataguard-test-primary ~]$

Solved it.解决了。

At the user input section, when trying to just hit 'enter' to pick the default value, the variable was being replaced with the value null because it read nothing, so I added an if after each user input to verify if the user just hit Enter and then fill the variable again with the default value.在用户输入部分,当尝试点击“输入”来选择默认值时,变量被替换为值null因为它什么也没读,所以我在每个用户输入后添加了一个if来验证用户是否刚刚点击输入然后用默认值再次填充变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM