1

I need assistance with creating an automatic ID for the Employee's when the other information is inputted by the user, is anyone able to help? I don't want the Employee ID to be asked to be inputted by the user, I would like it to be automatically generated and inputted into that field. I also have to program it in python, I can't use a SQL program or anything. Feel free to change any parts of the code, it's pretty flimsy at the moment, I've been messing around with the 'rowid' code for a bit trying to get that to work and I can't figure it out, many thanks.

import sqlite3


def AddEmployee():
    FirstName = input("Plaese enter the
                      employee's First Name: ")
    LastName = input("Please enter the
                     employee's Last Name: ")
    DName = input("Please Enter the employee's
                  Department Area: ")
    Gender = input("Please enter the employee's
                   Gender: ")
    Phone = int(input("Please enter the
                      employee's phone number: "))
    Address1 = input("Please Enter the
                     employee's Address1: ")
    Town = input("Please Enter the employee's
                 Town: ")
    Postcode = input("Please Enter the
                     employee's Postcode: ")
    DOB = input("Please Enter the employee's
                Date of Birth: ")
    HireDate = input("Please Enter the
                     employee's Date of Employment: ")

    db = sqlite3.connect("Database.db")
    cursor = db.cursor()
    cursor.execute("INSERT INTO Employees
                   VALUES(rowid, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                   (EmployeeID, FirstName, LastName, DName,
                       Gender, Phone, Address1, Town, Postcode,
                       DOB, HireDate))
    db.commit()
    cursor.close()


db = sqlite3.connect("Database.db")
cursor = db.cursor()
cursor.execute("""CREATE TABLE IF NOT 
EXISTS Employees
(EmployeeID integer PRIMARY KEY 
AUTOINCREMENT, DName integer,
FirstName text, LastName text, Gender text,                 
Phone text,
Address1 text, Town text, Postcode text,         
DOB Date,
HireDate Date)""")

cursor.execute("""CREATE TABLE IF NOT     
EXISTS Salaries
(SalaryID integer PRIMARY KEY 
AUTOINCREMENT, EmployeeID integer,
DepartmentDI integer, RegisterID integer,     
FirstName text,
LastName text, Address1 text, Town text, 
Postcode text, DOB Date,
HireDate Date, SalaryAmount integer, 
DhourlyRate integer,
DOvertimeHourlyRate integer)""")

cursor.execute("""CREATE TABLE IF NOT 
EXISTS Register
(RegisterID integer PRIMARY KEY 
AUTOINCREMENT, EmployeeID integer,
Date Date, Time Time, Present Boolean, 
HoursWorked integer,
OvertimeWorked integer)""")

cursor.execute("""CREATE TABLE IF NOT 
EXISTS Departments
(DepartmentID integer PRIMARY KEY 
AUTOINCREMENT, DName text, DQuota integer,
DHourlyRate integer, DovertimeHourlyRate 
integer)""")

db.commit()
cursor.close()
AddEmployee()
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • How do you want the employee id? Should it be random? Or sequential? What are the requirements? – Vinu Chandran Feb 15 '19 at 10:02
  • Take a look at [UUID](https://docs.python.org/3/library/uuid.html) see [this](https://stackoverflow.com/questions/534839/how-to-create-a-guid-uuid-in-python) question – Jab Feb 15 '19 at 10:08
  • Possible duplicate of [How to create a GUID/UUID in Python](https://stackoverflow.com/questions/534839/how-to-create-a-guid-uuid-in-python) – Jab Feb 15 '19 at 10:09
  • If you don't include an `INTEGER PRIMARY KEY` column in the ones being `INSERT`ed, a number will be automatically assigned to it. See https://www.sqlite.org/autoinc.html – Shawn Feb 15 '19 at 10:13

1 Answers1

0

you can do this

id = odm.SymbolField(primary_key=True)

If you dont have a field with a Primary Key than python create it automaticly.

Léon Zimmermann
  • 123
  • 1
  • 14