-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
99 lines (65 loc) · 2.39 KB
/
Copy pathmain.py
File metadata and controls
99 lines (65 loc) · 2.39 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sqlite3
conn = sqlite3.connect('employee.db')
c = conn.cursor()
### create table employees inside the file employee.db(only run one time)
'''
c.execute (""" CREATE TABLE employees (
id INTEGER PRIMARY KEY AUTOINCREMENT ,
first text ,
last text ,
pay integer
) """ )
'''
### creating function to insert new employe and delete getbyname :
def insert_emp(emp):
with conn:
c.execute("INSERT INTO employees VALUES(?,?,?,?)", (emp))
def get_emp_by_name(name):
name='%'+name.lower()+'%' #transform to lower case and partial search
c.execute("SELECT * FROM employees WHERE LOWER(last) LIKE :last" ,{'last':name })
return (c.fetchall())
def update_pay(emp,pay):
print (emp[0])
print(pay)
with conn:
c.execute("""UPDATE employees SET pay = :pay
WHERE first = :first AND last = :last""",
{'first': emp[0], 'last': emp[1], 'pay': pay})
def remove_emp(emp):
print(emp[1])
with conn:
c.execute("DELETE from employees WHERE first = :first AND last = :last",
{'first':emp[0],'last':emp[1]})
### creating variable emp_1 ...2..3 for employee to add in datafile:
emp_1=('Houssam','Rassi',10000)
emp_2=('jane','doe',9000)
#print(emp_1[0]) #print first position in emp_1
### inserting data in the table employees
#c.execute("INSERT INTO employees VALUES('corey','shafer',5000)")
### inserting in table variable emp_2
#c.execute("INSERT INTO employees VALUES(?,?,?)",(emp_2))
### select row in the table :
#c.execute("SELECT * FROM employees WHERE last = 'Rassi'")
c.execute("SELECT COUNT(*) FROM employees ")
row_nmbr=((c.fetchall()))
next_id= ((row_nmbr[(0)])[0])+2
print(next_id)
### inserting using the def function below:
#insert_emp([next_id,'Rony','abdel samad',9000])
### updating pay column of emp_1 using def function at the beginning
#update_pay(['khara','kaleb'], 27000)
### remove employee from database using first and last name in emp
#remove_emp(['joseph','balawand'])
### select a row in table using name(last name) caseinsensitiv and containing with def fonction at the beginning
emp=get_emp_by_name("la")
print (emp)
conn.commit()
### to print all the employee table :
c.execute("select * from employees")
all_rows = c.fetchall()
for row in all_rows:
print (row)
#print(c.fetchone())
#print(c.fetchall()) # result in a list
conn.commit()
conn.close()