如何从数据库中获取user_money值,以便使用 /货币命令输出?

发布于 2025-01-30 19:52:16 字数 1431 浏览 2 评论 0原文

如何从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不知在何时 2025-02-06 19:52:16

bot.send_message(message.chat.id,'ВашLISouth可:', { { user_money }

bot.send_message(message.chat.id, 'Ваше бабло:', {user_money})

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文