我正在创建一个telegrambot,用户可以在其中创建他们想要出售的商品的报价。当用户一个接一个地创建它时,它工作得非常好。但当他们同时这么做时,数据就被破坏了。它们在某些字段中得到NULL或覆盖。你知道吗
我使用了SQLite,我读到SQLite可能是问题所在,所以我转向MySQL(mysql.connector接口)还有这个问题。你知道吗
connMembers = mysql.connector.connect(host = "localhost", user = "root", password = "qwerty", database = "testdb")
connItems = mysql.connector.connect(host = "localhost", user = "root", password = "qwerty", database = "testdb")
def create_table_members():
cursor = connMembers.cursor()
sql = "CREATE TABLE IF NOT EXISTS Members" \
"(FirstName TEXT, LastName TEXT, ChatID INTEGER UNIQUE)"
cursor.execute(sql)
connMembers.commit()
cursor.close()
def create_table_items():
cursor = connItems.cursor()
sql = "CREATE TABLE IF NOT EXISTS " \
"Items(FirstName TEXT, LastName TEXT, ChatID INTEGER, " \
"ItemName TEXT, ItemPrice INTEGER, ItemID INTEGER PRIMARY KEY )"
cursor.execute(sql)
connMembers.commit()
cursor.close()
我是这样进入的:
@bot.message_handler(commands=['CreateOffer'])
def handle_createoffer(message):
msg = bot.send_message(message.from_user.id, "Enter Item Name")
bot.register_next_step_handler(msg, get_item_name)
完成此步骤后,将我重定向到此处:
def get_item_name(message):
global CurrentID
CurrentID = CurrentID + 1
ItemName = message.text
items_add(message.from_user.first_name, message.from_user.last_name, message.from_user.id, ItemName, 0, CurrentID)
msg = bot.send_message(message.from_user.id, 'What is your price?')
bot.register_next_step_handler(msg, get_item_price)
然后:
def get_item_price(message):
try:
ItemPrice = message.text
if ItemPrice.isdigit():
edit_item_price(ItemPrice, CurrentID)
else:
msg = bot.send_message(message.from_user.id, "not a digit")
bot.register_next_step_handler(msg, get_item_price)
except Exception as e:
msg = bot.send_message(message.from_user.id, "ERROR" )
bot.register_next_step_handler(msg, get_item_price)
要更新价格,我使用以下命令:
def edit_item_price(ItemPrice, ItemID):
cursor = connItems.cursor()
cursor.execute("UPDATE Items SET ItemPrice = %s WHERE ItemID = %s", (ItemPrice, ItemID))
connItems.commit()
cursor.close()
UPD:这是我获取当前ID的方式:
CurrentID = item_id_finder()
def item_id_finder():
cursor = connItems.cursor()
cursor.execute("SELECT MAX(ItemID) FROM Items")
data = cursor.fetchall()
connItems.commit()
cursor.close()
if data[0][0] == None:
return 0
else:
return data[0][0]
示例:
User1 ItemName : ItemNameOne
User2 ItemName : ItemNameTwo
User2 ItemPrice : 40
User1 ItemPrice : 20
Expected output:
User1 - ItemNameOne - 20
User2 - ItemNameTwo - 40
Real OutPut:
User1 - ItemNameOne - 0
User2 - ItemNameTwo - 20
你的问题是
global CurrentID
。这意味着所有用户共享一个值。你知道吗考虑以下流程:
所以,第24项的最终价格是由最后一个用户设定的,同时,第23项根本不会更新,所以它的价格可能仍然是空的(或者不管你的默认值是什么)。你知道吗
因此,首先,您需要更改
get_item_price
,将ID作为参数,而不是使用全局:然后需要传入正确的值:
但是,如何才能以一种不受其他同时工作的人干扰的方式获得这种
ItemID
?你知道吗实际上我对电报一无所知。如果它是通过协同程序工作的,那么您可以确定在
CurrentID = CurrentID + 1
和第一个可能阻塞的调用(可能是消息发送)之间没有被中断,所以您可以这样做:另一方面,如果它是通过线程工作的,而线程可以随时中断,则需要某种同步机制,如锁:
正如OP在评论中所建议的,如果您可以更改数据库模式,那么这里有一个更好的解决方案,只需使ItemID自动递增。在MySQL中,这是:
然后,更改} 属性权限:
INSERT
语句,将ItemID
保留在指定列之外,并在INSERT
之后获取^{您仍然需要传递
ItemID
,以便稍后UPDATE
语句可以使用它(或者,如果您愿意的话,传递光标本身),但是您不再需要担心以并发安全的方式使用锁和必须在任何线程启动之前执行的初始化函数来挑选数字等等。你知道吗相关问题 更多 >
编程相关推荐