Answer the question
In order to leave comments, you need to log in
Throws sqlite3.OperationalError: no such column: t1.user_id. What to do?
I make a referral link in a telegram bot through telebot. Created a table, an error that it cannot find a column, even with this t1. though in syntax of titles of columns it is impossible to put an end. What to do???
main.py file
import telebot
from models import *
import sqlite3
from telebot import types
bot = telebot.TeleBot('токен')
ref_link = 'https://telegram.me/{}?start={}'
conn = sqlite3.connect('users.db')
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS users(
user_id TEXT,
lname TEXT,
gender TEXT);
""")
conn.commit()
@bot.message_handler(commands=['start'])
def start_message(message):
print('1')
user_id = message.chat.id
splited = message.text.split()
if not Users.user_exists(user_id):
Users.create_user(user_id)
if len(splited) == 2:
Users.increase_ref_count(splited[1])
bot.reply_to(message, text='hello')
@bot.message_handler(commands=['ref'])
def get_my_ref(message):
bot_name = bot.get_me().username
bot.reply_to(message, text=ref_link.format(bot_name, message.chat.id))
ois = ref_link.format(bot_name, message.chat.id)
bot.send_message(message.chat.id, ois)
@bot.message_handler(commands=['ref_count'])
def get_my_refs(message):
count = Users.get_ref_count(message.chat.id)
bot.reply_to(message, text=f'Count: {count}')
if __name__ == '__main__':
bot.polling(none_stop=True)
from peewee import *
db = SqliteDatabase('users.db')
class BaseModel(Model):
class Meta:
database = db
class Users(BaseModel):
user_id = IntegerField(unique=True)
ref = IntegerField(default=0)
@classmethod
def get_user(cls, user_id):
return cls.get(user_id == user_id)
@classmethod
def get_ref_count(cls, user_id):
return cls.get(Users.user_id == user_id).ref
@classmethod
def increase_ref_count(cls, user_id):
user = cls.get_user(user_id)
user.ref += 1
user.save()
@classmethod
def user_exists(cls, user_id):
query = cls().select().where(cls.user_id == user_id)
return query.exists()
@classmethod
def create_user(cls, user_id):
user, created = cls.get_or_create(user_id=user_id)
Answer the question
In order to leave comments, you need to log in
You have an error in
@classmethod
def user_exists(cls, user_id):
query = cls().select().where(cls.user_id == user_id) # В этой строке, скорее всего cls не верный
return query.exists()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question