2017-02-25 29 views
0

我想從MSSQL數據導入到Python的 - Odoo安裝。我可以用下面的代碼導入一個字段。這有效,但我想檢索除名稱之外的其他字段。MSSQL到Python

#Retrieve data through recordset 
RecCount =rs.RecordCount 

print RecCount 

while not rs.EOF: 
    # print rs.Fields.item('Description').value 
    # print rs.Fields.item('Price').value 
    name = rs.Fields.item('Description').value 
    record = {'name' : name} 

    filter = [[['name' ,'=', name]]] 
    product_id = OdooApi.execute_kw(database, uid, pwd, 'product.template', 'search', filter) 

    if not product_id: 
     print " Create - " + name 
     resultset = OdooApi.execute_kw(database, uid, pwd, 'product.template', 'create', [record]) 

    else: 
     print "Already in table - " + name 



    rs.Move(1) 

我想導入其他字段,如條形碼字段。下面是我嘗試過的,但我得到一個錯誤。

#Retrieve data through recordset 
RecCount =rs.RecordCount 

print RecCount 

while not rs.EOF: 
    # print rs.Fields.item('Description').value 
    # print rs.Fields.item('Price').value 
    name = rs.Fields.item('Description').value 
    barcode = rs.Fields.item('ItemLookupCode').value 
    record = {'name' : name} 
    recordbarcode = {'barcode' : barcode} 

    filter = [[['barcode' ,'=', barcode]]] 
    product_id = OdooApi.execute_kw(database, uid, pwd, 'product.template', 'search', filter) 

    if not product_id: 
     print " Create - " + barcode 
     resultset = OdooApi.execute_kw(database, uid, pwd, 'product.template', 'create', ['record']['recordbarcode']) 

    else: 
     print "Already in table - " + barcode 



    rs.Move(1) 

我與上面的代碼得到的錯誤是

Traceback (most recent call last): 
    File "importdataorg.py", line 58, in <module> 
    resultset = OdooApi.execute_kw(database, uid, pwd, 'product.template', 'create', ['record']['recordbarcode']) 
TypeError: list indices must be integers, not str 
+0

你覺得不管'[ '記錄'] [ 'recordbarcode']'做的名單? –

+0

我不知道我希望能插入條形碼字段。我正在測試/學習 – user2379186

+0

此處沒有字段。這是兩個字符串和兩個列表 –

回答

0

我不知道你的錯誤是在所有有關你使用任何工具。

['record']['recordbarcode']不是有效的Python。

只要運行單獨的REPL,並list indices must be integers, not str

你想使用這些變量?

record = {'name' : name} 
recordbarcode = {'barcode' : barcode} 

如果是這樣,record['name']是有效的,但是這是真的只是在已有name變量。

也許你希望這本詞典

{'name' : name, 'barcode' : barcode} 

或者兩個庫

[record, recordbarcode] 
+1

謝謝{'name':name,'barcode':barcode}工作。 – user2379186

+0

如果我想在記錄已經存在的情況下更新一行,或者如果根據條形碼尚不存在,則創建一個新記錄。我試過的ResultSet = OdooApi.execute_kw(數據庫,UID,PWD, 'product.template', '更新',[記錄]) – user2379186

+0

如果你有一個新的問題,請創建新帖子 –