S
S
StepanKoliada2021-02-19 22:36:34
Python
StepanKoliada, 2021-02-19 22:36:34

Why is the value not being output from sqlite3?

"Your balance" is not displayed (def enter()):

import sqlite3


global db
global sql
db = sqlite3.connect('pdftea.db')
sql = db.cursor()

sql.execute("""CREATE TABLE IF NOT EXISTS users (
  login TEXT,
  cash INT
)""")
db.commit()

def tea():
  global ulogin
  ulogin = "Sergey"
  number = input("Сколько грамм: ")	
  if number == '40':
    sql.execute('UPDATE users SET cash = cash + 40 WHERE login = ?',(ulogin, ))
    print('Ваш баланс повышен на 40')
    db.commit()

  if number == '100':
    print('Ваш баланс повышен на 100')
    sql.execute('UPDATE users SET cash = cash + 100 WHERE login = ?',(ulogin, ))
    db.commit()
        
def enter():
  for i in sql.execute('SELECT cash FROM users'):
    print("Ваш баланс" + i)
    db.commit()
def main():
  enter()
  tea()
main()


balance should be displayed, but an empty string is displayed:
d:\python>pdf.py
How many grams: 100
Your balance is increased by 100

d:\python>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alan Gibizov, 2021-02-20
@phaggi

Ruslan. absolutely right. Also, there is no login “Sergey” by default, so if it was not previously added to the database, .fetchall() will return an empty list and still nothing will be printed.

R
Ruslan., 2021-02-19
@LaRN

First you need to execute the query
sql.execute('SELECT cash FROM users')
3 And then get the dataset
for i in sql.fetchAll()
Well, after receiving the data, you don't need to do
db.commit()
You haven't changed anything and there's nothing to fix.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question