P
P
pythonnovi4ok2020-10-12 20:22:20
Python
pythonnovi4ok, 2020-10-12 20:22:20

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

2 answer(s)
S
Sergey Karbivnichy, 2020-10-12
@hottabxp

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('Вы обычный пользователь')

Base structure:
5f849558dd33d864306206.png
PS:
How to make the database search for all tables where the status is 3.
do you have multiple tables? What for?

A
Alexander, 2020-10-12
@shabelski89

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');

then we make a request to find admins:
select u.username from users u 
left join rules r
ON u.status=r.id
where id=3;

face result.5f8498be9255b803207255.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question