如何对sqlite3数据库中的数据执行简单的数学运算?

zaq34kh6  于 2023-01-05  发布在  SQLite
关注(0)|答案(1)|浏览(191)

有一个数据库,其中有几列包含数字。2如何提取这些数字并对它们执行简单的数学运算:乘、除、加、减enter image description here
我该怎么做呢?我附上了整个程序的代码我很抱歉代码设计上的错误,我是学编程的第二天

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()
iqxoj9l9

iqxoj9l91#

sqlite处理算术运算符。
示例:

  • 第一个月
  • SELECT (summ + date) * (percent / 100) from my_table

相关问题