conn = sqlite3.connect(':memory:')
conn = sqlite3.connect('customer.db')
c = conn.cursor()
c.execute(""" CREATE TABLE customers (
first_name text,
last_name text,
email text
)""")
(""" """)
- Null
- Integer
- Real
- Text
- Blob
conn.commit()
conn.close()
c.execute("INSERT INTO customers VALUES ('John', 'Doe', 'john@email.com')")
customers = [
('Jason', 'Bourne', 'jason@email.com'),
('James', 'Bond', 'james@email'),
('Red', 'Sparrow', 'red@email'),
('Nathan', 'Muir', 'nathan@email.com')
]
c.executemany("INSERT INTO customers VALUES (?,?,?)", customers)
c.execute("SELECT * FROM customers")
print(c.fetchall())
c.execute("SELECT * FROM customers")
items = c.fetchall()
for item in items:
print(item)
c.execute("SELECT rowid, * FROM customers")
items = c.fetchall()
for item in items:
print(item)
rowid
in the select statement
c.execute("SELECT * FROM customers WHERE last_name = 'Bond'")
items = c.fetchall()
for item in items:
print(item)
# Using the like operator
c.execute("SELECT * FROM customers WHERE last_name like 'B%' ")
new_query = c.fetchall()
for i in new_query:
print(i)
# Updating Records
c.execute(""" UPDATE customers SET first_name = "Andy"
WHERE last_name = "Doe"
""")
# Commiting changes made
conn.commit()
c.execute("DELETE FROM customers WHERE rowid = 5 ")
c.execute("SELECT rowid, * FROM customers ORDER BY rowid DESC")
# Query using AND
c.execute("SELECT rowid, * FROM customers WHERE email LIKE '%@email' AND first_name LIKE 'Ja%' ")
items = c.fetchall()
for item in items:
print(item)
# Query using OR
c.execute("SELECT rowid, * FROM customers WHERE email LIKE '%@email' OR first_name LIKE 'Ja%' ")
query = c.fetchall()
for i in query:
print(i)
c.execute("SELECT rowid, * FROM customers LIMIT 2 ")
# Drop table
c.execute("DROP TABLE customers")
# Commiting changes made
conn.commit()