Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. import sqlite3
  2.  
  3. def menu():
  4. print()
  5. print()
  6.  
  7. print("\t\t Employee Database")
  8. print()
  9. print("a. View All Employees: ")
  10. print()
  11. print("b. Add a New Employee: ")
  12. print()
  13. print("c. Update an Employee: ")
  14. print()
  15. print("d. Delete an Employee: ")
  16. print()
  17. print("q. Quit")
  18. print()
  19. choice = input("Enter your choice: ")
  20. return choice[0].lower()
  21.  
  22. def DB_Connect():
  23. try:
  24. global conn
  25. global cur
  26. conn = sqlite3.connect("company.db")
  27. cur = conn.cursor()
  28. # cur.execute("drop table if exists employee")
  29. cur.execute("CREATE TABLE if NOT exists employee \
  30. (empID INTEGER PRIMARY KEY AUTOINCREMENT,\
  31. empName VARCHAR (25) NOT NULL,\
  32. empJob VARCHAR (25),\
  33. empSal DECIMAL (7, 2) NOT NULL, \
  34. empImgLoc VARCHAR (25),\
  35. empStatus tinyint(1) NOT NULL DEFAULT 1)")
  36. conn.commit()
  37. # conn.close()
  38. except Error as e:
  39. print(e)
  40.  
  41.  
  42.  
  43. def Add(name, job, sal, imageloc):
  44. cur.execute("insert into employee\
  45. values (null,?,?,?,?,?)",\
  46. (name, job, sal, imageloc,
  47. 1))
  48. conn.commit()
  49. cur.execute("SELECT name FROM sqlite_master WHERE type ='table'")
  50. print(cur.fetchall())
  51. View()
  52.  
  53.  
  54. def View():
  55. conn = sqlite3.connect("company.db")
  56. cur = conn.cursor()
  57. cur.execute('SELECT * FROM employee')
  58. rows = cur.fetchall()
  59. conn.close()
  60.  
  61. def update(id,name,job,sal,image,status):
  62. print(id,name,job,sal,image,status)
  63. conn = sqlite3.connect("emp.db")
  64. cur=conn.cursor()
  65. cur.execute("UPDATE employee set empName=?, empJob =?,\
  66. empSal =?, empImgLoc = ?, empStatus =? \
  67. WHERE empID=?", (name,job,sal,image,status,id))
  68. conn.commit()
  69.  
  70. ##def Update(id, name, job, sal, image, status):
  71. ## print(id, name, job, sal, image, status)
  72. ## conn = sqlite3.connect("emp.db")
  73. ## cur = conn.cursor()
  74. ## cur.execute("UPDATE employee set empName = ?, empJob = ?, empSal = ?, empImgLoc = ?, empStatus = ? \
  75. ## WHERE empID = ?", (name, job, sal, image, status, id))
  76. ## conn.commit()
  77.  
  78.  
  79. def delete(id):
  80. cur.execute("delete from employee where empID=?", (id,))
  81. conn.commit()
  82.  
  83. def Search(id="", name=""):
  84. conn = sqlite3.connect("company.db")
  85. cur = conn.cursor()
  86. cur.execute("SELECT * FROM employee WHERE empID= ? or empName = ?", (id, name))
  87. conn.commit()
  88. conn.close()
  89.  
  90. ##def insert(name,job,sal,imageloc):
  91. ## conn = sqlite3.connect("emp.db")# no need to execute dbconn() - why?
  92. ## cur=conn.cursor()
  93. ## cur.execute("insert into employee values
  94. ## (null,?,?,?,?,?)",(name,job,sal,imageloc,1))
  95. ## conn.commit()
  96. ## conn.close()
  97. ##
  98. def main():
  99. DB_Connect()
  100. cur.execute("SELECT name FROM sqlite_master WHERE type = 'table'")
  101. print(cur.fetchall())
  102.  
  103.  
  104. while True:
  105. choice = menu()
  106.  
  107. if choice == 'a':
  108. print()
  109. print("View All Selected")
  110. print()
  111. View()
  112.  
  113.  
  114. elif choice == 'b':
  115. print()
  116. print("Add a New Employee Selected")
  117. Add('test', 'tester', 12344, 'img/test1.png')
  118. elif choice == 'c':
  119. print()
  120. print("Update an Employee Selected")
  121. print()
  122. update(id,name,job,sal,image,status)
  123.  
  124. elif choice == 'd':
  125. print()
  126. print("Delete an Employee Selected")
  127. print()
  128. Delete(id)
  129.  
  130. elif choice == 's':
  131. print()
  132. print("Search an Employee Selected")
  133. print()
  134. Search(id="", name="")
  135.  
  136. elif(choice == 'q'):
  137. print()
  138. print("you quit, bye")
  139. print()
  140. break
  141.  
  142. else:
  143. print()
  144. print("invalid choice - try again")
  145. print()
  146. conn.close()
  147. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement