任务是:sqlite中有一个数据库,假设格式如下:
ID - Name - Price - Shop
1 - Milk - 3.5 - Testshop
2 - Banana - 2.3 - Testshop2
3 - Water - 0.5 - Testshop
我正在尝试编写一个电报机器人(使用 Telebot、pytelegrambotapi),它允许您查看此列表并在数据库中对其进行编辑。目前,我可以获取所有记录,也可以向用户询问他要编辑的行的 ID,例如:
Bot: Enter the line number to edit:
User: 1
Bot: What parameter do you want to change?
User: /Price
Bot: Enter new value:
User: 1.1
cur.execute ("UPDATE products SET name = {} WHERE id = {}". format (name, id))
另外,如果用户想继续改变这个列表,比如改变店铺的名字,那么就不用再问他要ID了,他必须保存在某个地方,也就是用户干脆例如,输入命令 / shop 并编辑同一行(编号为 1)并将 shop 参数更改为其他内容。
这是我的文件中的一个示例:
def change_options(name, id):
try:
cur.execute("UPDATE products SET name = {} WHERE id = {}".format(name,id))
conn.commit()
return print("OK")
except:
return print("Error!")
电报机器人代码:
@bot.message_handler(commands=['options_list'])
def start_message(message):
bot.send_message(message.chat.id, str(get_all_lists_name()))
sent = bot.send_message(message.chat.id, 'Enter # list')
bot.register_next_step_handler(sent, get_list_options)
def get_list_options(message):
id = message.text
bot.send_message(message.chat.id, str(get_all_options(id))) # ID From User
现在,我不知道如何更改选项:
@bot.message_handler(commands=['milk'])
def change_milk(message):
change_options(milk, id) #Where i can get this id, from "def get_list_options(message):"
bot.send_message(message.chat.id, str(get_all_lists_name()))
bot.send_message(message.chat.id, 'milk changed')
@bot.message_handler(commands=['banana'])
def change_banana(message):
change_options(banana, id) #Where i can get this id, from "def get_list_options(message):"
bot.send_message(message.chat.id, str(get_all_lists_name()))
bot.send_message(message.chat.id, 'banana changed')
对于可能不正确的演示,我深表歉意,我已尽力解释,感谢您的回答!