如何从数据库中获取user_money值,以便使用 /货币命令输出?
如何从sqlite数据库中获取user_money值并使用 /货币命令输出? 有必要在输入 /货币命令时,来自SQLITE数据库的值是输出的。如何实施它?我在pytelegrambotapi的Python上写。
import telebot
import sqlite3
bot = telebot.TeleBot('5107117113:AAFKBPtDjw_ZCNn7agSUhIfHSXal5vN_dds')
@bot.message_handler(commands=['start'])
def start(message):
connect = sqlite3.connect('users.db')
cursor = connect.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS login_id(
id INTEGER, money INTEGER
)""")
connect.commit()
people_id = message.chat.id
cursor.execute(f"SELECT id FROM login_id WHERE id = {people_id}")
data = cursor.fetchone()
if data is None:
user_id = message.chat.id
user_money = '10000'
cursor.execute("INSERT INTO login_id VALUES(?, ?);", (user_id, user_money))
connect.commit()
bot.send_message(message.chat.id, 'Вы успешно зарегестрирован. Ваш баланс: 10000 рублей.')
else:
bot.send_message(message.chat.id, 'Вы уже в базе')
@bot.message_handler(commands=['money'])
def money(message):
connect = sqlite3.connect('users.db')
cursor = connect.cursor()
user_money = cursor.fetchall()
people_id = message.chat.id
cursor.execute(f"SELECT user_money FROM login_id WHERE id = {people_id}")
bot.send_message(message.chat.id, 'Ваше бабло:', user_money)
bot.polling()
```[enter image description here][1]
[1]: https://i.sstatic.net/esVHk.jpg
How do I get the user_money value from the SQLite database and output it with the /money command?
it is necessary that when entering the /money command, the value from the sqlite database is output. How to implement it? I write in python, pyTelegramBotAPI.
import telebot
import sqlite3
bot = telebot.TeleBot('5107117113:AAFKBPtDjw_ZCNn7agSUhIfHSXal5vN_dds')
@bot.message_handler(commands=['start'])
def start(message):
connect = sqlite3.connect('users.db')
cursor = connect.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS login_id(
id INTEGER, money INTEGER
)""")
connect.commit()
people_id = message.chat.id
cursor.execute(f"SELECT id FROM login_id WHERE id = {people_id}")
data = cursor.fetchone()
if data is None:
user_id = message.chat.id
user_money = '10000'
cursor.execute("INSERT INTO login_id VALUES(?, ?);", (user_id, user_money))
connect.commit()
bot.send_message(message.chat.id, 'Вы успешно зарегестрирован. Ваш баланс: 10000 рублей.')
else:
bot.send_message(message.chat.id, 'Вы уже в базе')
@bot.message_handler(commands=['money'])
def money(message):
connect = sqlite3.connect('users.db')
cursor = connect.cursor()
user_money = cursor.fetchall()
people_id = message.chat.id
cursor.execute(f"SELECT user_money FROM login_id WHERE id = {people_id}")
bot.send_message(message.chat.id, 'Ваше бабло:', user_money)
bot.polling()
```[enter image description here][1]
[1]: https://i.sstatic.net/esVHk.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
bot.send_message(message.chat.id,'ВашLISouth可:', { { user_money } )
bot.send_message(message.chat.id, 'Ваше бабло:', {user_money})