From 950c29436fbea73fbdb49aec5dba338e9753a1db Mon Sep 17 00:00:00 2001 From: Corey Schafer Date: Mon, 17 Apr 2017 21:33:03 -0600 Subject: [PATCH] SQLite Tutorial --- .gitignore | 2 ++ Python-SQLite/employee.db | Bin 0 -> 8192 bytes Python-SQLite/employee.py | 19 +++++++++++++ Python-SQLite/snippets.txt | 30 ++++++++++++++++++++ Python-SQLite/sqlite_demo.py | 52 +++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+) create mode 100644 Python-SQLite/employee.db create mode 100644 Python-SQLite/employee.py create mode 100644 Python-SQLite/snippets.txt create mode 100644 Python-SQLite/sqlite_demo.py diff --git a/.gitignore b/.gitignore index f14c0de..e51980b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .DS_Store +__pycache__/ +*.pyc # Video Scripts s.txt diff --git a/Python-SQLite/employee.db b/Python-SQLite/employee.db new file mode 100644 index 0000000000000000000000000000000000000000..01f1b509be7819d6cd0ab7c6bd46a41d58d20c7c GIT binary patch literal 8192 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU|@n`1}I=;U|?W@vOyGx52Ep5CI;P~ z9lQ(-42=AR4E%-ol#a@chQMeDjE2By2#kinXb6mkz-S1JhQMeDjE2By2#kinFbaW2 z9!7R?d3nah^peD+oYd6Zf}H%y)YM`)gULC_)iK0XA;i(i$5jC?tf0ZAfCSPqi;7DW zN>VFIbWp@|60r&wBvvY9=9Q$Trxu|o*M#)VUo-H(9!6n2>Xp$D7!85Z5Eu=C(GVC7 vfzc2c4S~@R7!85Z5Eu=C(GVE9At21iA}-DBoL`h$8JwJvn3h_^a5w+}@832` literal 0 HcmV?d00001 diff --git a/Python-SQLite/employee.py b/Python-SQLite/employee.py new file mode 100644 index 0000000..1697294 --- /dev/null +++ b/Python-SQLite/employee.py @@ -0,0 +1,19 @@ + +class Employee: + """A sample Employee class""" + + def __init__(self, first, last, pay): + self.first = first + self.last = last + self.pay = pay + + @property + def email(self): + return '{}.{}@email.com'.format(self.first, self.last) + + @property + def fullname(self): + return '{} {}'.format(self.first, self.last) + + def __repr__(self): + return "Employee('{}', '{}', {})".format(self.first, self.last, self.pay) diff --git a/Python-SQLite/snippets.txt b/Python-SQLite/snippets.txt new file mode 100644 index 0000000..c2e8ec4 --- /dev/null +++ b/Python-SQLite/snippets.txt @@ -0,0 +1,30 @@ + +def insert_emp(emp): + pass + + +def get_emps_by_name(lastname): + pass + + +def update_pay(emp, pay): + pass + + +def remove_emp(emp): + pass + + + + +def update_pay(emp, pay): + with conn: + c.execute("""UPDATE employees SET pay = :pay + WHERE first = :first AND last = :last""", + {'first': emp.first, 'last': emp.last, 'pay': pay}) + + +def remove_emp(emp): + with conn: + c.execute("DELETE from employees WHERE first = :first AND last = :last", + {'first': emp.first, 'last': emp.last}) diff --git a/Python-SQLite/sqlite_demo.py b/Python-SQLite/sqlite_demo.py new file mode 100644 index 0000000..b02bd48 --- /dev/null +++ b/Python-SQLite/sqlite_demo.py @@ -0,0 +1,52 @@ +import sqlite3 +from employee import Employee + +conn = sqlite3.connect(':memory:') + +c = conn.cursor() + +c.execute("""CREATE TABLE employees ( + first text, + last text, + pay integer + )""") + + +def insert_emp(emp): + with conn: + c.execute("INSERT INTO employees VALUES (:first, :last, :pay)", {'first': emp.first, 'last': emp.last, 'pay': emp.pay}) + + +def get_emps_by_name(lastname): + c.execute("SELECT * FROM employees WHERE last=:last", {'last': lastname}) + return c.fetchall() + + +def update_pay(emp, pay): + with conn: + c.execute("""UPDATE employees SET pay = :pay + WHERE first = :first AND last = :last""", + {'first': emp.first, 'last': emp.last, 'pay': pay}) + + +def remove_emp(emp): + with conn: + c.execute("DELETE from employees WHERE first = :first AND last = :last", + {'first': emp.first, 'last': emp.last}) + +emp_1 = Employee('John', 'Doe', 80000) +emp_2 = Employee('Jane', 'Doe', 90000) + +insert_emp(emp_1) +insert_emp(emp_2) + +emps = get_emps_by_name('Doe') +print(emps) + +update_pay(emp_2, 95000) +remove_emp(emp_1) + +emps = get_emps_by_name('Doe') +print(emps) + +conn.close()