-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbmethods.rb
67 lines (60 loc) · 1.76 KB
/
dbmethods.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
module DbMethods
include DB
def add_to_table(name, role)
values = [name, role]
begin
connect("./employee.db")
sql_statement("INSERT into #{role.capitalize} (Name, Role) VALUES (?, ?)")
sql_execute(values)
rescue SQLite3::Exception => e
reporterr(e)
ensure
close_db()
end
end
def retrieve_employees(role)
begin
connect("./employee.db")
sql_statement("select * from #{role.capitalize}")
res = sql_execute()
display_employees(res, role)
rescue SQLite3::Exception => e
reporterr(e)
ensure
close_db()
end
end
def display_employees(list, role)
puts "LIST OF #{role.upcase()} \n"
len = 0
#list.each {|e| len = e[0].length if len < e[0].length}
print "Name".ljust(60) + "Role\n"
list.each {|e| puts " #{e[0].capitalize}".ljust(60) + " #{e[1].capitalize}"}
puts "\n#{"-"*20}"
end
def update_employee(name, role, oldname)
values = [name, role, oldname]
begin
connect("employee.db")
sql = "UPDATE #{role.capitalize} SET Name = ?, Role = ? WHERE Name = ?"
sql_statement(sql)
sql_execute(values)
rescue SQLite3::Exception => e
reporterr(e)
ensure
close_db()
end
end
def del_employee(name, role)
begin
connect("./employee.db")
sql = "DELETE FROM #{role.capitalize} WHERE Name = (?)"
sql_statement(sql)
sql_execute(name)
rescue SQLite3::Exception => e
reporterr(e)
ensure
close_db()
end
end
end