To translate the given ER diagram into Python code using SQLite3, we can create and populate the necessary database tables and classes. Here's the Python code to accomplish this:
import sqlite3
class City:
def __init__(self, id: int, name: str, description: str):
self.id = id
self.name = name
self.description = description
class District:
def __init__(self, id: int, name: str, population: int):
self.id = id
self.name = name
self.population = population
class Landmark:
def __init__(self, id: int, name: str, type: str, description: str):
self.id = id
self.name = name
self.type = type
self.description = description
class Tourist:
def __init__(self, id: int, name: str, nationality: str):
self.id = id
self.name = name
self.nationality = nationality
class Street:
def __init__(self, id: int, name: str, description: str):
self.id = id
self.name = name
self.description = description
class Vendor:
def __init__(self, id: int, name: str, goodsSold: str):
self.id = id
self.name = name
self.goodsSold = goodsSold
class Bridge:
def __init__(self, id: int, name: str, yearBuilt: int):
self.id = id
self.name = name
self.yearBuilt = yearBuilt
class River:
def __init__(self, id: int, name: str, length: float):
self.id = id
self.name = name
self.length = length
# Create a connection to the SQLite database
conn = sqlite3.connect('city_database.db')
c = conn.cursor()
# Create tables
c.execute('''
CREATE TABLE IF NOT EXISTS city (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS district (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
population INTEGER NOT NULL,
city_id INTEGER NOT NULL,
FOREIGN KEY (city_id) REFERENCES city(id)
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS landmark (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type TEXT NOT NULL,
description TEXT,
district_id INTEGER NOT NULL,
FOREIGN KEY (district_id) REFERENCES district(id)
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS street (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
district_id INTEGER NOT NULL,
FOREIGN KEY (district_id) REFERENCES district(id)
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS tourist (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
nationality TEXT NOT NULL
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS vendor (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
goodsSold TEXT NOT NULL,
street_id INTEGER NOT NULL,
FOREIGN KEY (street_id) REFERENCES street(id)
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS river (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
length FLOAT NOT NULL
);
''')
c.execute('''
CREATE TABLE IF NOT EXISTS bridge (
id INTEGER PRIMARY