Skip to content

Commit 950c294

Browse files
committedApr 18, 2017
SQLite Tutorial
1 parent 3417b19 commit 950c294

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.DS_Store
2+
__pycache__/
3+
*.pyc
24

35
# Video Scripts
46
s.txt

‎Python-SQLite/employee.db

8 KB
Binary file not shown.

‎Python-SQLite/employee.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
class Employee:
3+
"""A sample Employee class"""
4+
5+
def __init__(self, first, last, pay):
6+
self.first = first
7+
self.last = last
8+
self.pay = pay
9+
10+
@property
11+
def email(self):
12+
return '{}.{}@email.com'.format(self.first, self.last)
13+
14+
@property
15+
def fullname(self):
16+
return '{} {}'.format(self.first, self.last)
17+
18+
def __repr__(self):
19+
return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay)

‎Python-SQLite/snippets.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
def insert_emp(emp):
3+
pass
4+
5+
6+
def get_emps_by_name(lastname):
7+
pass
8+
9+
10+
def update_pay(emp, pay):
11+
pass
12+
13+
14+
def remove_emp(emp):
15+
pass
16+
17+
18+
19+
20+
def update_pay(emp, pay):
21+
with conn:
22+
c.execute("""UPDATE employees SET pay = :pay
23+
WHERE first = :first AND last = :last""",
24+
{'first': emp.first, 'last': emp.last, 'pay': pay})
25+
26+
27+
def remove_emp(emp):
28+
with conn:
29+
c.execute("DELETE from employees WHERE first = :first AND last = :last",
30+
{'first': emp.first, 'last': emp.last})

‎Python-SQLite/sqlite_demo.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import sqlite3
2+
from employee import Employee
3+
4+
conn = sqlite3.connect(':memory:')
5+
6+
c = conn.cursor()
7+
8+
c.execute("""CREATE TABLE employees (
9+
first text,
10+
last text,
11+
pay integer
12+
)""")
13+
14+
15+
def insert_emp(emp):
16+
with conn:
17+
c.execute("INSERT INTO employees VALUES (:first, :last, :pay)", {'first': emp.first, 'last': emp.last, 'pay': emp.pay})
18+
19+
20+
def get_emps_by_name(lastname):
21+
c.execute("SELECT * FROM employees WHERE last=:last", {'last': lastname})
22+
return c.fetchall()
23+
24+
25+
def update_pay(emp, pay):
26+
with conn:
27+
c.execute("""UPDATE employees SET pay = :pay
28+
WHERE first = :first AND last = :last""",
29+
{'first': emp.first, 'last': emp.last, 'pay': pay})
30+
31+
32+
def remove_emp(emp):
33+
with conn:
34+
c.execute("DELETE from employees WHERE first = :first AND last = :last",
35+
{'first': emp.first, 'last': emp.last})
36+
37+
emp_1 = Employee('John', 'Doe', 80000)
38+
emp_2 = Employee('Jane', 'Doe', 90000)
39+
40+
insert_emp(emp_1)
41+
insert_emp(emp_2)
42+
43+
emps = get_emps_by_name('Doe')
44+
print(emps)
45+
46+
update_pay(emp_2, 95000)
47+
remove_emp(emp_1)
48+
49+
emps = get_emps_by_name('Doe')
50+
print(emps)
51+
52+
conn.close()

0 commit comments

Comments
 (0)
Please sign in to comment.