-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhereClause.py
More file actions
27 lines (20 loc) · 799 Bytes
/
whereClause.py
File metadata and controls
27 lines (20 loc) · 799 Bytes
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
import sqlite3
# Connect to database
conn = sqlite3.connect("customers.db")
# Create a cursor
c = conn.cursor()
# Query the database (db)
# We want to fetch only certain things from the db, so we use the where function
c.execute("SELECT * FROM customers WHERE first_name LIKE 'customer%' ")
# A comparison operator used a lot is: LIKE / which compares to similar results and have to end with % or start with %
# Example: WHERE email LIKE '%customer'
# We can also use all of the other comparison operators like: >, <, ==, >=, etc.
items = c.fetchall()
# Formatting the results
for item in items:
print("Name: " + item[0] + "\tLast Name: " + item[1] + "\tEmail: " + item[2])
# We also can just do: print(item)
# Commit changes to db
conn.commit()
# Close connection to db
conn.close()