Commit
·
31812e1
1
Parent(s):
57c4258
Created initialize_db.py to initialize database with sample data and cleared db.py
Browse files- data/initialize_db.py +49 -0
data/initialize_db.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
|
| 3 |
+
DB_PATH = "database.sqlite"
|
| 4 |
+
|
| 5 |
+
def get_connection():
|
| 6 |
+
return sqlite3.connect(DB_PATH)
|
| 7 |
+
|
| 8 |
+
def create_tables():
|
| 9 |
+
conn = get_connection()
|
| 10 |
+
cursor = conn.cursor()
|
| 11 |
+
|
| 12 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS Employees(
|
| 13 |
+
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 14 |
+
Name TEXT NOT NULL,
|
| 15 |
+
Department TEXT NOT NULL,
|
| 16 |
+
Salary INTEGER NOT NULL,
|
| 17 |
+
Hire_Date TEXT NOT NULL
|
| 18 |
+
)
|
| 19 |
+
''')
|
| 20 |
+
|
| 21 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS Departments(
|
| 22 |
+
ID INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 23 |
+
Name TEXT NOT NULL,
|
| 24 |
+
Manager TEXT NOT NULL
|
| 25 |
+
)
|
| 26 |
+
''')
|
| 27 |
+
|
| 28 |
+
employees = [
|
| 29 |
+
(1, 'Alice', 'Sales', 50000, '2021-01-15'),
|
| 30 |
+
(2, 'Bob', 'Engineering', 70000, '2020-06-10'),
|
| 31 |
+
(3, 'Charlie', 'Marketing', 60000, '2022-03-20')
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
departments = [
|
| 35 |
+
(1, 'Sales', 'Alice'),
|
| 36 |
+
(2, 'Engineering', 'Bob'),
|
| 37 |
+
(3, 'Marketing', 'Charlie')
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
cursor.executemany('INSERT INTO Employees VALUES (?,?,?,?,?)', employees)
|
| 41 |
+
cursor.executemany('INSERT INTO Departments VALUES (?,?,?)', departments)
|
| 42 |
+
|
| 43 |
+
conn.commit()
|
| 44 |
+
conn.close()
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
create_tables()
|