import sqlite3
database = "hotel.db"
# 1- Create table
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
email TEXT
)
"""
)
# 2- Add 2 users on the table
hotel_users = [("Paula", "paula@email.com"), ("Victor", "victor@email.com")]
try:
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
cursor.executemany(
"""
INSERT INTO users (name, email) VALUES(?, ?)
""",
hotel_users,
)
except ConnectionError as error:
print(f"Connection error: {error}")
# 3- Read informations on the table
try:
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
cursor.execute(
"""
SELECT * FROM users
"""
)
users = cursor.fetchall()
for user in users:
print(user)
except ConnectionError as error:
print(f"Connection error: {error}")