2015-01-14 57 views
0

我想從數據庫返回一個值,並得到這個錯誤。我嘗試了以前回答的問題,但沒有運氣。誰能幫助我?TypeError:只能連接元組(不是「int」)到元組

@frappe.whitelist() 
def generate_barcode(): 

    last_barcode = frappe.db.sql("""\ 
     select MAX(barcode) from `tabItem` """) 

    if last_barcode: 
     last_barcode = last_barcode + 1 

    else: 
     x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] 
     random.shuffle(x) 
     last_barcode = x[0] 



    return {'last_barcode':last_barcode} 

將回溯:

Traceback (innermost last): 
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/app.py", line 49, in application 
response = frappe.handler.handle() 
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 66, in handle 
execute_cmd(cmd) 
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/handler.py", line 89, in execute_cmd 
ret = frappe.call(method, **frappe.form_dict) 
File "/home/adminuser/frappe-bench-hitech/apps/frappe/frappe/__init__.py", line 531, in call 
return fn(*args, **newargs) 
File "/home/adminuser/frappe-bench-hitech/apps/erpnext/erpnext/stock/doctype/item/item.py", line 405, in generate_barcode 
last_barcode = last_barcode + 1 
TypeError: can only concatenate tuple (not "int") to tuple 
+0

'last_barcode'在'if'和'select'之間包含什麼? – RedX

+0

它包含一串數字。 – LaksHmiSekhar

回答

1

我以某種方式得到了答案。感謝大家的幫助。

@frappe.whitelist() 
def generate_barcode(): 

last_barcode_auto = frappe.db.sql("""\ 
    select MAX(barcode) from `tabItem` """) 

if last_barcode_auto[0][0] : 
    last_barcode = last_barcode_auto[0][0] 
    final_barcode= last_barcode+1 

else: 
    final_barcode=random.randrange(100001, 100000000000, 2) 



return {'final_barcode':final_barcode} 
1

我不知道什麼是「冰咖啡」是,你沒有發佈完整的回溯,所以我們只能儘量和猜測,但很顯然frappe.db.sql("select MAX(barcode) from TabItem的")回報一個元組(這是我從一個SQL數據庫一個選擇期望的),所以你想是這樣的:

row = frappe.db.sql(
    "select MAX(barcode) from `tabItem`" 
    ) 

last_barcode = row[0] 
if last_barcode: 
    last_barcode = last_barcode + 1 

作爲一個方面說明:如果你想有一個隨機INT在0和9之間(包括),拼寫爲random.randint(0, 9)

+0

或'random.randrange(10)' – zehnpaard

+0

我試過這個仍然得到相同的錯誤,而且,Frappe是一個框架。[docs](https://frappe.io/apps/frappe-framework) – LaksHmiSekhar

0

錯誤消息是說last_barcode是一個元組。使用last_barcode[0]來檢索第一個值(在這種情況下,這是唯一的值,因爲您選擇了一列)。

if last_barcode: 
    last_barcode = last_barcode[0] + 1 
相關問題