Answer the question
In order to leave comments, you need to log in
How to do a check for the presence of a specific number in a database (SQLite)?
I have a
sqlite
database, it has the following columns: telegram_id,
username
,
status ('You are a regular user')
Answer the question
In order to leave comments, you need to log in
Here I sketched the simplest code for you, play around with it. There is nothing complicated here:
import sqlite3
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
name = input('Введите имя пользователя: ')
cursor.execute('SELECT status FROM Users WHERE username = ?',(name,))
result = cursor.fetchone()[0]
if result == 3:
print('Вы администратор')
else:
print('Вы обычный пользователь')
How to make the database search for all tables where the status is 3.do you have multiple tables? What for?
For good, you need 2 tables and a foreign key :
create table `rules` (`id` INT PRIMARY KEY, `rule` VARCHAR(50));
insert into `rules` (`id`, `rule`) VALUES (1, 'user');
insert into `rules` (`id`,`rule`) VALUES (2, 'moderator');
insert into `rules` (`id`,`rule`) VALUES (3,'admin');
create table `users` (`telegram_id` INT PRIMARY KEY, `username` VARCHAR(50), `status` INT, FOREIGN KEY (`status`) REFERENCES `rules` (`id`));
insert into `users` (`telegram_id`, `username`, `status`) VALUES (555, 'user555', '1');
insert into `users` (`telegram_id`, `username`, `status`) VALUES (666, 'user666', '2');
insert into `users` (`telegram_id`, `username`, `status`) VALUES (777, 'user777', '3');
insert into `users` (`telegram_id`, `username`, `status`) VALUES (888, 'user888', '3');
select u.username from users u
left join rules r
ON u.status=r.id
where id=3;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question