2011-07-04 23 views
1

對象的'wb'(以及顯然所有上級對象)加載並保存信息,但是獲取數據庫物理的樹模型根本沒有加載。到目前爲止,互聯網搜索讓我感覺到了bipkis。 (我打算編寫一個例程來測試某些列的存在,並以編程方式添加適當的觸發器,我使用正則表達式和數據庫導出計算出了一個kludge,但這有點兒不合適)。在MySQL Workbench 5.2.34的Python腳本中,grt.root.wb沒有物理模型

缺少某種形式的簡單激活? (連接也許)

下失敗,錯誤 「AttributeError的: 'NoneType' 對象有沒有屬性 'physicalModels'」:

# -*- coding: utf-8 -*- 
# MySQL Workbench Python script 
# <description> 
# Written in MySQL Workbench 5.2.34 

import grt 
#import mforms #?? 
stOut="" 
stTrigger=""" 
delimiter $$ 
create trigger `docdb_mk2`.tsi_{t} before insert on `docdb_mk2`.`{t}` 
for each row begin 
set new.inserted=now(); 
end$$""" 

# iterate through all tables from schema""" 
schema = grt.root.wb.doc.physicalModels[0].catalog.schemata[0] 
for table in schema.tables: 
    #print table.name 
    #if table. 
    #check to make sure both inserted and Updated are in the table 
    #Since I have no clue how to do that in here, I'll skip it for now. 
    stOut=stOut+stTrigger(t=table.name) 

即使失敗:

# -*- coding: utf-8 -*- 
# MySQL Workbench Python script 
# <description> 
# Written in MySQL Workbench 5.2.34 
import grt 
#import mforms 
# iterate through all schemas 

for schema in grt.root.wb.doc.physicalModels[0].catalog.schemata: 
    print schema.name 

請注意,我現在不會就我的SQL尋求建議,僅僅是在腳本編寫中這個令人惱怒的障礙。

編輯:顯然,通過在UI中加載這些元素來加載一些對象。如果有某種方法讓它們加載其他方式,我會很高興知道,但是看起來如果從主工作臺屏幕加載腳本模塊,您會看到一個部分初始化的wb對象。如果您從不同的子系統(如數據建模模塊,(腳本>運行工作臺腳本文件...))加載其他部件,則其他部件將加載並開始工作。嗯。

回答

2

雖然這不是一個完整的解決方案,但這是我的工作正在努力解決這個問題。從我可以告訴wb.docs.physicalModels對象僅在'設計/模型'模式下可用時,我需要做的是在SQL編輯器模式下運行腳本。下面的代碼確實執行查詢並返回結果,但我正在努力處理與目前在對象中看到的文檔不匹配的文檔。但願這是一些幫助

import grt 

for editor in grt.root.wb.sqlEditors: 
    # add some text to the 'Output' tab 
    editor.addToOutput("Test", 0) 

    results = editor.executeScript("show tables") 
    #print results.rowCount() 
    # the above throws an exception because what is returned from executeScript() differs from the docs 
    print results 

    for buffer in editor.queryBuffers: 
     print buffer.replaceContents("test") 
+0

所以......它似乎很重要你如何_load_腳本編輯器。 如上所述,我遇到了麻煩。但是,在閱讀完這些之後,我從模型編輯器中加載了腳本編輯器,並且沒有大驚小怪。 –

+0

我接受這是最好的答案,因爲通過它我發現了一個部分解決方案,但我仍然在想。 –

1

您必須從您的工作臺,打開一個模型(* .mwb文件),或者如果不存在,創建一個 (選擇「從現有數據庫中創建EER模型」)。這應該會讓你的錯誤消失。

2

我找到了一個解決方案:

print results.rowCount() 
# the above throws an exception because what is returned from executeScript() differs from the docs 

,如果你寫你對第一個元素聲明中db_query_Resultset列表,而這將工作()。
I.e. print results[0].rowCount的作品。

相關問題