0

For starters I'll just say that I'm fresh Python programmer. I started writing database client and I have problem. Perhaps this question for many will seem silly, but for me as a rookie is it a problem.

I write main_module that adds data to the database I would like to after the last condition, user have the possibility to add more data. Something like that: repeat = input("Do you wanna create new record?(Y/N)") if repeat = Y: (the program starts from the beginning) else: (Program close)

Where such a condition should be found?

Below is my code

import sqlite3
import time
import datetime
import sys

conn = sqlite3.connect('template.db')
c = conn.cursor()

def create_table(): #
    c.execute("CREATE TABLE IF NOT EXISTS Theft(template1 TEXT, template2 TEXT, template3 TEXT, template4 TEXT, template5 TEXT)")


def data_entry():
    unix = int(time.time())
    template1 = str(datetime.datetime.fromtimestamp(unix).strftime('%Y-%m-%d %H:%M:%S'))

    c.execute("INSERT INTO Theft(template1, template2, template3, template4, template5) VALUES (?, ?, ?, ?, ?)",
                (template1, template2, template3, template4, template5))

    conn.commit()

template2 = input("ENTER template ")
template3 = input("ITEM template ")
template4 = input("INPUT template ")
template5 = input("INPUT YOUR template: ")

accept = input("Do you wanna create new record in DB? (Y/N)")
if accept == "y":
    create_table(), data_entry()
elif accept == "Y":
    create_table(), data_entry()
else:
    sys.exit(0)


c.close()
conn.close()

Regards Jmazure :)

Jmazure
  • 1
  • 1
  • Firstly, please pick a relevant title and tags for your question; your issue has nothing to do with "writing a database client", nor with sqlite. Secondly, please search for the many many times this question has been asked before. – Daniel Roseman Oct 05 '16 at 10:54

1 Answers1

0

You have to put your input procedure inside an infinite loop that will be broken only if the user input something else than "y" or "Y":

while True:
    accept = input("Do you wanna create new record in DB? (Y/N)")
    if accept.lower() == "y": <-- checks if accept =="y" or accept == "Y"
        create_table(), data_entry()
    else:
        break
JMat
  • 752
  • 6
  • 14