Loading notes...
Loading notes...
B.Tech • Chapter 11
B.Tech level module on Database Connectivity & Data Science Foundations.
Modern engineering applications interact with databases and process large datasets. Python acts as glue code, connecting SQL databases (SQLite, PostgreSQL) and utilizing C-optimized libraries like NumPy and Pandas for data manipulation.
Advanced Engineering Concepts
The sqlite3 module allows embedded relational database management without a separate server process. To prevent SQL injection attacks, parameterized queries must ALWAYS be used. For data processing, NumPy provides N-dimensional array objects and broadcast operations that are orders of magnitude faster than Python lists because the underlying loops are executed in C. Pandas builds on this to provide DataFrames for tabular data analysis.
Code Implementation
import sqlite3
import numpy as np
# Secure Database Execution
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute("CREATE TABLE students (id INT, name TEXT, grade REAL)")
# Parameterized query to prevent SQL Injection
data_to_insert = [(1, "Alice", 9.2), (2, "Bob", 8.5)]
cursor.executemany("INSERT INTO students VALUES (?, ?, ?)", data_to_insert)
# NumPy Performance
# Vectorized operations avoid slow Python for-loops
python_list = [1, 2, 3, 4, 5]
numpy_array = np.array(python_list)
print("NumPy Vectorized Multiply:", numpy_array * 10)
conn.close()