This guide provides instructions on how to set up the database, modify the database name, provide credentials, and run the provided script (Insertion.py) to input data into the database.
- MySQL Server: Ensure you have a MySQL server installed and running.
- Python: Install Python (version 3.x or above).
- Required Python Library: Install
mysql-connectorfor database interaction.
In the script, replace the placeholder database name my_database with the desired database name. Update the following line accordingly:
USE my_database;For example, if your database name is trucking_db, change it to:
USE trucking_db;When connecting to the database in the script, ensure you input your MySQL username and password in the connection setup. The script should look something like this:
import mysql.connector
connection = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="root",
password="your_password",
database="trucking_db"
)Replace your_password with your MySQL credentials.
Run the SQL schema provided in this guide in your MySQL environment:
- Open your MySQL Workbench or terminal.
- Copy the entire SQL schema from this document,
ex2.sql - Paste it into your SQL editor or command line.
- Execute the script to create all the tables.
Install the mysql-connector library using pip:
pip install mysql-connectorTo populate the database with sample data or perform insert operations, run the Insertion.py script:
python Insertion.pyAfter running the script, a new text file named database_population_log.txt will be created in the same directory as the script. This file will contain a detailed log of all the data populated into the database, including any success or error messages for each table.
Example contents of database_population_log.txt:
2024-11-20 14:08:03,752 - Connected to MySQL Trucking database
2024-11-20 14:08:03,754 - Inserted Driver 1 with driverID 1
...
2024-11-20 14:08:03,972 - Inserted SupplierContactInfo for supplierID 36: Contact Person: John Jones, Contact Number: 7729709344
2024-11-20 14:08:03,973 - Data population complete and committed to the database.
2024-11-20 14:08:03,973 - MySQL connection closed
This addition makes it clear that a log file will be generated and explains its purpose.
- Ensure the database connection details in the
Insertion.pyscript match your MySQL setup. - Check for any dependencies or additional scripts required for your project to function as intended.
- Use the
localhostserver unless you have a specific remote database server to connect to.