Home > Mobile >  how to perform simple mathematical operations with data from the sqlite3 database?
how to perform simple mathematical operations with data from the sqlite3 database?

Time:01-07

there is a database with several columns containing numbers. How to pull out these numbers and perform simple mathematical operations with them: multiply, divide. add, subtract enter image description here

how can I do this? I attach the entire program code I'm sorry for the mistakes in the design of the code, I'm learning to program for the second day

from tkinter import *
import sqlite3 as sl
# Connecting the database
conn = sl.connect('my_fin.db')
cur = conn.cursor()

# Creating tables
cur.execute("""CREATE TABLE IF NOT EXISTS my_table(
   № INT PRIMARY KEY,
   Name TEXT,
   Summ INT,
   Date INT,
   Percent INT);
""")
conn.commit()
# GRAPHICAL INTERFACE
window = Tk()
window.title("Мои финансы")
# Fields for entering values
txt11 = Entry(window, width=10)
txt11.grid(column=1, row=1)
txt22 = Entry(window, width=10)
txt22.grid(column=2, row=1)
txt33 = Entry(window, width=10)
txt33.grid(column=3, row=1)
txt44 = Entry(window, width=10)
txt44.grid(column=4, row=1)
txt55 = Entry(window, width=10)
txt55.grid(column=5, row=1)

txta = Entry(window, width=10)
txta.grid(column=1, row=2)
txtb = Entry(window, width=10)
txtb.grid(column=2, row=2)
txtc = Entry(window, width=10)
txtc.grid(column=3, row=2)
txtd = Entry(window, width=10)
txtd.grid(column=4, row=2)
txtf = Entry(window, width=10)
txtf.grid(column=5, row=2)

# Buttom
def clicked1():
    value = [(txt11.get(), txt22.get(), txt33.get(), txt44.get(), txt55.get())]
    cur.executemany("INSERT INTO my_table VALUES(?, ?, ?, ?, ?);", value)
    conn.commit()
    
def clicked2():
    cur.execute("SELECT * FROM my_table;")
    all_results = cur.fetchall()
    print(all_results)
def clicked3():
    entered = txta.get()
    cur.execute("SELECT * FROM my_table WHERE № = ?", [entered])
    all_results = cur.fetchall()
    print(all_results) 
 
btn = Button(window, text="enter", command=clicked1)
btn.grid(column=1, row=3)
btn1 = Button(window, text="Show", command=clicked2)
btn1.grid(column=2, row=3)
btn2 = Button(window, text="Show2", command=clicked3)
btn2.grid(column=3, row=3)
window.mainloop()

CodePudding user response:

sqlite handles the arithmetic operators.

Examples:

  • SELECT summ date from my_table

  • SELECT (summ date) * (percent / 100) from my_table

  • Related