H
H
happyjuic2022-03-18 21:24:36
Python
happyjuic, 2022-03-18 21:24:36

"not working" sqlite3 database?

The bottom line is, I created a simple database, just to store logins and passwords entered through the console. But, when entering data and following from the output, only the last entered login is displayed, as if I store them not in the database, but in a simple variable. Who can tell me where is my mistake?
PS my code is:

import sqlite3

sql = sqlite3.Connection('enter.db')
c = sql.cursor()
print('Подключение к базе данных проведено успешно!')

c.execute("""CREATE TABLE IF NOT EXISTS users (userlogin TEXT, password INTEGER, cash INTEGER)""")


sql.commit()

userlogin = input('Логин: ')
password = input('Пароль: ')

c.execute("SELECT userlogin FROM users")
if c.fetchone() is None:
    c.execute('INSERT INTO users VALUES (?, ?, ?)', (userlogin, password, 0))
else:
    print('Такой логин уже занят')
c.execute("SELECT * FROM users")
print(c.fetchall())

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dr. Bacon, 2022-03-18
@bacon

We read about the basics of sql, specifically here about WHERE, although due to a misunderstanding of the problem, it is better to go through the entire course of the basics.

A
Alan Gibizov, 2022-03-18
@phaggi

Who can tell me where is my mistake?

The error is in the word “displayed”, it is correct to write “displayed”, the test word is “what does it do”.
And after sending the command to change in sql (execute), it would probably be nice to save the changes with the commit command.
In addition, when checking for the presence of a login, it is necessary to request a selection taking into account the desired login.
import sqlite3

sql = sqlite3.Connection('enter.db')
c = sql.cursor()
print('Подключение к базе данных проведено успешно!')

c.execute("""CREATE TABLE IF NOT EXISTS users (userlogin TEXT, password INTEGER, cash INTEGER)""")


sql.commit()

userlogin = input('Логин: ')
password = input('Пароль: ')

c.execute("SELECT userlogin FROM users where (?)", (userlogin,))
if c.fetchone() is None:
    c.execute('INSERT INTO users VALUES (?, ?, ?)', (userlogin, password, 0))
    sql.commit()
else:
    print('Такой логин уже занят')
c.execute("SELECT * FROM users")
print(c.fetchall())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question