Python Forum
Perminantly saving user inputs + being able to retrieve it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Perminantly saving user inputs + being able to retrieve it
#1
To whom it may concern,

Good afternoon, I have recently started a side project of mine to see how far I could stretch my knolage in python and I hit a prety large wall. I am trying to make a kind of user system to be able to save a users name + pin and be able to recall it, It saves a them inside of a new fine, but as soon as I run the program again it overwrites it. I do not even know how to get startted on recalling user inputs from a separate file.

My spaghetti:

print("WELCOME TO JESUS BANKING")
def make_an_acc():
    print("Our bank is the strongest around!\n")
    print("Enter your name\n")
    newname = input("\n")
    print("Enter the most secure 4 digit code that you can think of \n")
    newpin = input("\n")
    name = open("Bank files/Account Info/Accounts","w");
    name.write(newname + " " + newpin)
    name.close()
    print("Enter the amount you want to deposit\n")
    newamount = int(input("\n"))
    return [newname, newpin, newamount]
def datastore(make_an_acc):
    name = open("Bank files/Account Info/Accounts","w");
    name.write(make_an_acc())
    name.close()
customer_details = make_an_acc()
Thank you for any help in advance.

Respectfully,

ThatOneGuyNoOneKnows
Reply
#2
To save the data and then retrieve it at a later date or time and execution of your program code you would need to setup a database to save the input data. If you are running the program from the shell then the program is saving the data within the run environment and will only be retained in the "variable" while the program is executed and running. Once you close the program the variable no longer holds the value.

If you are writing your code to use GUI or not you would need a database.

#============================= CONNECT TO DATABASE =============================
    conn = sqlite3.connect(Bank.db')
    c = conn.cursor()
#========================= CREATE DATABASE WITH TABLES =========================
    c.execute('''CREATE TABLE IF NOT EXISTS Bank(
    Name CHAR(50),
    Pin Int(4));''')
    conn.commit()
Write def statement to save the input data
def Save():
    try:
        saveInput = Name.get()
        c.execute('SELECT Name FROM Bank WHERE Name= :saveInput',
        {'saveInput': saveInput})
        conn.commit()
        saveResult = c.fetchone()
        if saveResult == None:
            c.execute('''INSERT INTO Bank VALUES (?,?)''',
            (newname.get(), (newpin.get())))
            conn.commit()
            messagebox.showinfo('Bank','Data saved successfully.')
            
        else:
            messagebox.showinfo(Bank,
            'That account name already exists in the database.')
    except:
        messagebox.showerror(Bank,'Database Error.')
This will show you how to create the database and to save the input data.
Once the data is saved you would need to call the database to retrieve the data when you run the program again or at a later time however depending on what you are trying to do... there are safe guards you might want to look at when saving the data and retrieving.

Hopefully this offer some insight how to properly save the data that is input so it can be retrieved when the program is closed and then run again at a later time.
"Often stumped... But never defeated."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write a response if a user inputs a string horuscope42 3 2,218 Apr-29-2020, 03:39 PM
Last Post: deanhystad
  Trying to prompt user for 2 inputs on one line pythonprogrammer 2 2,495 Sep-15-2019, 04:41 PM
Last Post: snippsat
  user inputs in constructing a dictionary Exsul 3 3,696 Apr-10-2019, 12:25 PM
Last Post: ichabod801
  unit testing a method that asks two user inputs() in console gebel 0 2,140 Apr-03-2019, 07:59 PM
Last Post: gebel
  question regarding user Inputs cibb 10 7,223 Apr-04-2017, 03:34 AM
Last Post: alicarlos13
  code that takes inputs for user name amounts etc and then sends custom message shaumyabrata 5 5,304 Feb-12-2017, 11:37 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020