两个用户同时输入时,SQL会覆盖数据(Python)

2024-09-30 05:25:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个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

Tags: textfromidmessagegetdefbotmsg
1条回答
网友
1楼 · 发布于 2024-09-30 05:25:05

你的问题是global CurrentID。这意味着所有用户共享一个值。你知道吗

考虑以下流程:

  • 用户1启动了一个offer,ID被提升到23。你知道吗
  • 当您等待用户1的价格时,用户2启动了一个报价,并且ID被提升到24。你知道吗
  • 当您等待用户1和用户2的价格时,其中一个响应,您将更新第24项的价格。你知道吗
  • 然后另一个响应,您再次更新第24项的价格。你知道吗

所以,第24项的最终价格是由最后一个用户设定的,同时,第23项根本不会更新,所以它的价格可能仍然是空的(或者不管你的默认值是什么)。你知道吗


因此,首先,您需要更改get_item_price,将ID作为参数,而不是使用全局:

def get_item_price(ItemID, message):
    # now use ItemID instead of CurrentID

然后需要传入正确的值:

    bot.register_next_step_handler(msg, functools.partial(get_item_price, ItemID))

但是,如何才能以一种不受其他同时工作的人干扰的方式获得这种ItemID?你知道吗

实际上我对电报一无所知。如果它是通过协同程序工作的,那么您可以确定在CurrentID = CurrentID + 1和第一个可能阻塞的调用(可能是消息发送)之间没有被中断,所以您可以这样做:

def get_item_name(message):
    global CurrentID
    CurrentID = CurrentID + 1
    ItemID = CurrentID
    # now use ItemID instead of CurrentID

另一方面,如果它是通过线程工作的,而线程可以随时中断,则需要某种同步机制,如锁:

id_lock = threading.Lock()

def get_item_name(message):
    global CurrentID
    with id_lock:
        CurrentID = CurrentID + 1
        ItemID = CurrentID
    # now use ItemID instead of CurrentID

正如OP在评论中所建议的,如果您可以更改数据库模式,那么这里有一个更好的解决方案,只需使ItemID自动递增。在MySQL中,这是:

ItemID INTEGER PRIMARY KEY AUTO_INCREMENT

然后,更改INSERT语句,将ItemID保留在指定列之外,并在INSERT之后获取^{}属性权限:

ItemID = cursor.lastrowid

您仍然需要传递ItemID,以便稍后UPDATE语句可以使用它(或者,如果您愿意的话,传递光标本身),但是您不再需要担心以并发安全的方式使用锁和必须在任何线程启动之前执行的初始化函数来挑选数字等等。你知道吗

相关问题 更多 >

    热门问题