0
conn = sqlite3.connect('School.db')
c = conn.cursor()
def account_Table(): #Creates the table for user accounts.
    c.execute('CREATE TABLE IF NOT EXISTS accounts(datestamp TEXT, u_id INT, username TEXT, password TEXT, classy TEXT, subject TEXT, forename TEXT, surname TEXT)')

def LoginFrame(Frame): #Builds the login/home screen for the program

        username_holder = Label(text="Username", bg="#b4c4a1", fg="#000000")
        password_holder = Label(text="Password", bg="#b4c4a1", fg="#000000")

        username = Entry()
        password = Entry(show="*")

        username_holder.grid(row=0, column=20)
        password_holder.grid(row=1, column=20)
        username.grid(row=0, column=40)
        password.grid(row=1, column=40)

        checkbox = Checkbutton(text="Keep me logged in", bg="#b4c4a1")
        checkbox.grid(row=3,column=40)

        logbtn = Button(text="Login", bg="#b4c4a1", command = Login_Check)
        logbtn.grid(row=3,column=30)

        sigup = Button(text="New User", bg="#b4c4a1", command = DataEntry)
        sigup.grid(row=3,column=20)

def DataEntry():
    datestamp = datetime.datetime.now() #Shows when the account was made
    u_id = (random.randrange(0,100)) #Gives the user a unique identifier, I don't really need it as the username is a pretty good identifier for this job.

    label_1 = Label(text='Enter A Username', bg="#b4c4a1")
    username = Entry()
    username.grid(row=4, column=40)
    label_1.grid(row=4, column=39)

    label_2 = Label(text='Enter A Password', bg="#b4c4a1")
    password = Entry(show='*')
    label_2.grid(row=5, column=39)
    password.grid(row=5, column=40)

    label_3 = Label(text='Enter Your Class', bg="#b4c4a1")
    classy = Entry()
    classy.grid(row=6, column=40)
    label_3.grid(row=6, column=39)

    subject = Entry()
    label_4=Label(text='Enter Your Subject', bg="#b4c4a1")
    subject.grid(row=7, column=40)
    label_4.grid(row=7, column=39)

    label_5=Label(text='Enter Your First Name', bg="#b4c4a1")
    forename = Entry()
    forename.grid(row=8, column=40)
    label_5.grid(row=8, column=39)

    surname = Entry()
    label_6 = Label(text='Enter Your Second Name', bg="#b4c4a1")
    surname.grid(row=9, column=40)
    label_6.grid(row=9, column=39)

All of the entries in 'DataEntry():' will go into the account table as a new record.

def Login_Check():
    print(".... Doing This Later")



#account_Table() 
root = Tk()
root.geometry('630x350')
root.config(bg="#b4c4a1")
root.title("Student Manager")
root.wm_iconbitmap('Icon.png')
LoginFrame(root)
Tk.mainloop

I don't know how to pass the entries from the Tkinter Login Box, as values to an SQL 'INSERT INTO' statement using sqlite3, It did work before using just the console as a method of inputting values, but that defeats the point of having a GUI for the user to use.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
SadfSadv
  • 5
  • 1
  • 5
  • Please make the title of your question short and precise. Move the rest of the text into the body of the post. – DYZ Jan 08 '17 at 00:20
  • Do *not* use string formatting as in the accepted answer, you're wide open for SQL injection attacks. The proper way to do it is to pass the strings as parameters to `execute`, which takes multiple parameters specifically for this reason; see https://stackoverflow.com/a/1633589/616460 for examples. – Jason C May 31 '17 at 13:04

1 Answers1

-1

You can use the get() function of the Entry widgets to get what's typed in them and then use string-formatting to generate your SQL-command.

# Python 2.7

import sqlite3 as sql
import tkinter as tk
import datetime

connection = sql.connect('School.db')
cursor = connecton.cursor()
# Sidenote: your database does not have a PRIMARY KEY
# I made u_id the PRIMARY KEY
cursor.execute('CREATE TABLE IF NOT EXISTS accounts(datestamp TEXT, ' \ 
    'u_id INT PRIMARY KEY, username TEXT, password TEXT, classy TEXT, ' \
    'subject TEXT, forename TEXT, surname TEXT)')
insert_command = """INSERT OR IGNORE INTO accounts(datestamp, username, """ \
                 """password, classy, subject, forename, surname) VALUES(""" \   
                 """'%s', '%s', '%s', '%s', '%s', '%s', '%s');"""

root = tk.Tk()
username_entry = tk.Entry(root)
password_entry = tk.Entry(root)
classy_entry = tk.Entry(root)
subject_entry = tk.Entry(root)
forename_entry = tk.Entry(root)
surname_entry = tk.Entry(root)

def insert_user():
    datestamp = datetime.datetime.now()
    username = username_entry.get()
    password = password_entry.get()
    classy = classy_entry.get()
    subject = subject_entry.get()
    forename = forename_entry.get()
    surname = surname_entry.get()
    cursor.execute(insert_command % (datestamp, username, password,
                                     classy, subject, forename, surname))

insert_button = tk.Button(root, text="Insert my account", command=insert_user)


username_entry.pack()
password_entry.pack()
classy_entry.pack()
subject_entry.pack()
forename_entry.pack()
surname_entry.pack()
insert_button.pack()

root.mainloop()
RedFantom
  • 332
  • 1
  • 10