-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqllite3-test.py
37 lines (23 loc) · 1.13 KB
/
sqllite3-test.py
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
import sqlite3
conn = sqlite3.connect('mycompany.db')
curObj = conn.cursor()
#Create TABLE only once not every time
curObj.execute("CREATE TABLE IF NOT EXISTS employees(id INTEGER PRIMARY KEY, name TEXT, salary REAL, department TEXT, postion TEXT)")
conn.commit()
curObj.execute("INSERT INTO employees VALUES(1,'Mike', 75000, 'Python', 'Software Developer')")
conn.commit() #commit is neceassary when ur modifiing anything in the db
#the ? and tuple method helps to prevent SQL injection attacks.
curObj.execute("INSERT INTO employees VALUES(?,?,?,?,?)",(1,'Jake',50000,'JS','Tester'))
conn.commit()
curObj.execute("SELECT * FROM employees")
results = curObj.fetchall()
print(results) #will give a list of all records aka [ (1,'Mike', 75000, 'Python', 'Software Developer') , (1,'Jake',50000,'JS','Tester')]
curObj.execute("SELECT name FROM employees WHERE salary > 50000")
results = curObj.fetchall()
print(results) # [ ('Mike') ]
curObj.execute("DELETE FROM employees")
curObj.commit() #deletes all records from table employees
curObj.execute("DELETE FROM employees WHERE name=?", ("Jake"))
conn.commit()
curObj.close()
conn.close()