Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sets the value to 'None' instead of 'absent' #730

Open
Danik-coder opened this issue Aug 1, 2024 · 0 comments
Open

sets the value to 'None' instead of 'absent' #730

Danik-coder opened this issue Aug 1, 2024 · 0 comments

Comments

@Danik-coder
Copy link

Danik-coder commented Aug 1, 2024

I'm writing a telegram bot. I can't understand why it sets the default value to NULL if 'absent' is specified. once again, the values are not specified anywhere. help

I'm uploading a question to github for the first time, so I'm sorry if I didn't fill out the question correctly.

here is the code

database.py

import sqlite3 as sq

async def db_start():
    db = sq.connect('usersProfile.db')
    cur = db.cursor()
    cur.execute('''
        CREATE TABLE IF NOT EXISTS userProfile (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER,
            name TEXT,
            user TEXT DEFAULT NULL,
            balance INTEGER DEFAULT 5000,
            car TEXT DEFAULT 'absent',
            house TEXT DEFAULT 'absent',
            yacht TEXT DEFAULT 'absent',
            phone TEXT DEFAULT 'absent',
            pc TEXT DEFAULT 'absent',
            pet TEXT DEFAULT 'absent',
            mining_farm INTEGER DEFAULT 0,
            mining_cash INTEGER DEFAULT 0,
            farms INTEGER DEFAULT 0,
            farms_cash INTEGER DEFAULT 0,
            married TEXT DEFAULT 'NO',
            Time_in_game INTEGER DEFAULT 0,
            root_level INTEGER DEFAULT 0
        )
    ''')
    db.commit()
    db.close()

async def db_set_profile(user_id, name, user):
    db = sq.connect('usersProfile.db')
    cur = db.cursor()
    cur.execute('''INSERT OR IGNORE INTO userProfile(user_id, name, user) VALUES (?, ?, ?)''', (user_id, name, user))
    db.commit()
    db.close()

async def check_user_exists(user_id):
    db = sq.connect('usersProfile.db')
    cur = db.cursor()
    cur.execute('SELECT COUNT(*) FROM userProfile WHERE user_id = ?', (user_id,))
    count = cur.fetchone()[0]
    db.close()
    return count > 0

Handlers.py

import asyncio
import logging
import sqlite3 as sq

from aiogram import Bot, Dispatcher, F, Router
from aiogram.filters import CommandStart, Command
from aiogram.types import Message, CallbackQuery

from database.database import db_set_profile, check_user_exists
import Keyboards.keyboards as kb



router = Router()


@router.message(CommandStart())

async def cmd_start(message: Message):
    user_id = message.from_user.id
    user_exists = await check_user_exists(user_id)
    if not user_exists:
        await db_set_profile(user_id, message.from_user.first_name, message.from_user.username)

    db = sq.connect('usersProfile.db')
    cur = db.cursor()

    cur.execute('SELECT user_id, name, balance, car, house, yacht, phone, pc, pet, mining_farm, mining_cash, farms, '
                'farms_cash, married, time_in_game, root_level FROM userProfile WHERE user_id = ?', (message.from_user.id,))
    info = cur.fetchone()
    cur.close()
    db.close()
    if info:
        (user_id, name, balance, car, house, yacht, phone, pc, pet, mining_farm, mining_cash, farms, farms_cash, married,
        time_in_game, root_level) = info
    await message.answer(f'''
hello {message.from_user.first_name}

    
🔽🔽🔽 your profile generated 🔽🔽🔽

USER_ID: {user_id}
Nickname: {name}
Balance: {balance}
Car: {car}
House: {house}
Yacht: {yacht}
Phone: {phone}
PC: {pc}
Pet: {pet}
Number of mining farm: {mining_farm}
Mining farm income: {mining_cash}
Number of farms: {farms}
Farm income: {farms_cash}
Marital status: {married}
Time in game: {time_in_game}
Root level: {root_level}
''', reply_markup=kb.main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant