self.itemCode.setText(str(getNumber()))
这会调用getNumber()来获取要添加到用户的下一个项目的项目ID:
def getNumber():
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('SELECT seq FROM sqlite_sequence')
itemNumber = c.fetchone()[0]
code = str(itemNumber + 1)
return code
早些时候,我开始工作了。使用上面的代码......我不知道发生了什么,但现在又回来了。
Traceback (most recent call last):
File "C:/Users/Lloyd/Desktop/Python Projects/stock/addItem.py", line 239, in <module>
ui = Ui_Dialog()
File "C:/Users/Lloyd/Desktop/Python Projects/stock/addItem.py", line 32, in __init__
self.setupUi(self)
File "C:/Users/Lloyd/Desktop/Python Projects/stock/addItem.py", line 176, in setupUi
self.retranslateUi(Dialog)
File "C:/Users/Lloyd/Desktop/Python Projects/stock/addItem.py", line 185, in retranslateUi
self.itemCode.setText(str(getNumber()))
File "C:/Users/Lloyd/Desktop/Python Projects/stock/addItem.py", line 26, in getNumber
return itemNumber + 1
TypeError: Can't convert 'int' object to str implicitly
答案 0 :(得分:0)
Hello lloydyu24,
试试这段代码,
def getNumber():
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('SELECT seq FROM sqlite_sequence')
itemNumber = c.fetchone()[0]
code = int(itemNumber) + 1
return str(code)
self.itemCode.setText(getNumber())
我希望我的回答很有帮助。 如果有任何疑问请评论。
答案 1 :(得分:0)
我解决了。因此,基本上,在添加1之前,需要先将itemNumber(我的sqlite数据库表中的序列号)转换为int。
def getNumber():
conn = sqlite3.connect('inventory.db')
c = conn.cursor()
c.execute('SELECT seq FROM sqlite_sequence')
itemNumber = c.fetchone()[0]
return int(itemNumber) + 1 #CHANGED THIS from "return str(itemNumber + 1)"
以前,它有效。我不知道发生了什么,但Python不再将itemNumber识别为int。
答案 2 :(得分:0)
itemNumber是一个字符串,所以在你的代码中:
code = str(itemNumber + 1)
您正在尝试将数字(1)添加到字符串(itemNumber)。 首先,您应该将字符串转换为int,然后将数字(1)添加到它:
code = str (int (itemNumber) + 1)