Home > Back-end >  User input SQL Lite and Python and store
User input SQL Lite and Python and store

Time:08-19

I need help I want the tax column to be calculated from the income column Also I need the information to be stored in the database from the user input.

import sqlite3

conn = sqlite3.connect('tax1.db')
c = conn.cursor()
#c.execute("""CREATE TABLE tax (
          #First text,
          #Last text,
          #Income real,
          #Tax real
          #)""")

#c.execute("INSERT INTO tax VALUES('Henry', 'Bass', 80000, 20000)")

for i in range(1):
         First = input("Please enter first name:")
         Last = input("Please enter last name:")
         Income = input("Please enter annual income:")
         conn.execute("INSERT INTO tax VALUES ('{}", "{}", 
         {})).format(First,Last,Income))

        c.execute("SELECT * FROM tax WHERE Last='Bass'")
        print(c.fetchall())
        conn.commit()
        conn.close()

CodePudding user response:

To have the tax calculated from the income I would suggest something like a lookup table:

tax_ratios = {"low_income": 0.1, "medium_income": 0.2, "big_income": 0.3, "huge_income": 0.45}

Then after the user has made his inputs you could check with if-statements to which catogorie he belongs:

if Income > 250000:
    tax_ratio = tax_ratios["huge_income"]
if Income < 250000 and Income > 10000:
    tax_ratio = tax_ratios["big_income"]
if Income < 10000 and Income > 70000:
    tax_ratio = tax_ratios["medium_income"]
if Income < 70000:
    tax_ratio = tax_ratios["low_income"]

Then you can multiply the income with the tax ratio and store it in the database:

tax_absolute = tax_ratio*Income
conn.execute("INSERT INTO tax VALUES ('{}", "{}", 
         {}, {})).format(First,Last,Income, tax_absolute))

Of course this easy lookup table is not complex enough to deal with complicated tax systems but it might be a good start.

If I may add something regarding the database safety: With formatting the SQL-Query as a string it is easy for an attacker to perform what is called SQL-Injection. You find a good explanationhere.

  • Related