CS Project
CS Project
CS Project
Python is a high-level, versatile, and interpreted programming language known for its
simplicity and readability. Created by Guido van Rossum and first released in 1991,
Python has gained widespread popularity due to its ease of use, extensive standard
library, and vibrant community support. Its syntax emphasizes code readability and
clarity, making it an excellent choice for both beginners and experienced developers.
Python's popularity is largely in part due to its active community of developers who
contribute to the language's growth by creating packages and libraries that extend its
capabilities. The language’s ease of learning makes it an ideal choice for beginners
while its versatility makes it a favorite among seasoned programmers as well
SQL offers several advantages that make it a powerful and widely-used language for
managing and interacting with relational database:
1) Data Integrity and Consistency:
SQL enforces data integrity by allowing you to define rules and relationships between
data elements. For instance, you can set up primary keys to ensure each record in a
table has a unique identifier. This prevents duplicate entries and maintains data
accuracy. Foreign keys establish relationships between tables, ensuring that data in
one table references valid data in another. Additionally, SQL supports constraints
that prevent inappropriate or inconsistent data from being entered. For example, you
can set a constraint to ensure that a birth date is always before the current date.
These features collectively maintain the consistency and reliability of your data.
2) Efficient Data Retrieval:
SQL databases use indexing mechanisms to speed up data retrieval. An index is like
a roadmap that helps the database quickly locate relevant rows. By creating indexes
on frequently queried columns, you significantly reduce the time it takes to find
specific data. This efficiency is especially crucial when dealing with large datasets.
Without indexing, queries would require scanning the entire dataset, resulting in
slower performance. SQL's indexing capabilities enhance data retrieval speed,
making applications more responsive and scalable.
3) Flexible Data Manipulation:
SQL provides a versatile set of commands for querying and manipulating data. The
SELECT statement is particularly powerful, allowing you to retrieve data based on
complex conditions, join tables, aggregate values, and sort results. This flexibility
enables you to tailor queries to your specific needs, extracting valuable insights from
your data. SQL's ability to perform calculations, summarizations, and transformations
directly within the database reduces the need for complex data manipulation in
external code. This not only streamlines your code but also improves performance
by leveraging the database engine's optimizations.
SQL has limitations, including challenges with handling extremely large datasets
efficiently, complexity in managing hierarchical data structures, rigidity in schema
changes, performance bottlenecks in complex joins, a learning curve for effective
usage, and potential vendor lock-in due to different RDBMS variations. Additionally,
for small projects, SQL's overhead might outweigh benefits. These disadvantages
can impact scalability, agility, and overall performance, urging consideration of
alternative database solutions for specific use cases.
SQL is utilized across a broad spectrum of applications, ranging from business and
finance to scientific research and web development, due to its ability to manage
structured data effectively and efficiently. Different relational database management
systems may implement SQL with variations in syntax, features, and optimizations.
INTEGRATING PYTHON WITH MYSQL
Integrating Python with MySQL involves utilizing libraries that facilitate communication
between Python programs and MySQL databases. One popular library for this purpose is
mysql-connector-python. Here's a concise guide to get you started:
Establish a Connection:
To connect to your MySQL database, provide the necessary connection details such as
hostname, username, password, and database name:
**mydb=mysql.connector.connect(host="localhost", user="username password="password",
database="databasename")
Create a Cursor:
Create a cursor object to interact with the database:
**mycursor = mydb.cursor()
Execute Queries:
You can execute SQL queries using the cursor's execute() method:
**mycursor.execute("SELECT * FROM tablename")
Fetch Results:
Retrieve the query results using methods like fetchone() or fetchall():
**result = mycursor.fetchall()
for row in result:
print(row)
Commit Changes:
If you modify the database, don't forget to commit the changes:
**mydb.commit()
Close Connection:
Finally, close the connection when you're done:
**mydb.close()
SOFTWARE USED
● Windows 10
● Python
● MYSQL
HARDWARE USED
import mysql.connector
def view_items(cursor):
cursor.execute('SELECT * FROM items')
all_items = cursor.fetchall()
for item in all_items:
print("ID:", item[0])
print("Name:", item[1])
print("Quantity:", item[2])
print("Price:", item[3])
print("--------------------------")
def search_item(cursor):
find_item = input("Enter the item's name to search in inventory: ")
cursor.execute('SELECT * FROM items WHERE name = %s',
(find_item,))
found_item = cursor.fetchone()
if found_item:
print('The item named', find_item, 'is displayed below with its
details:')
print("ID:", found_item[0])
print("Name:", found_item[1])
print("Quantity:", found_item[2])
print("Price:", found_item[3])
else:
print('Item not found.')
if found_item:
print('Here are the current details of', item_name)
print("ID:", found_item[0])
print("Name:", found_item[1])
print("Quantity:", found_item[2])
print("Price:", found_item[3])
while True:
print('------------------Welcome to the supermarket------------------')
print('1. View items\n2. Add items for sale\n3. Purchase items\n4. Search
items\n5. Edit items\n6. Exit')
choice = input('Enter the number of your choice: ')
if choice == '1':
view_items(cursor)
● Class 11 NCERT
● Class 12 NCERT
● Computer Science with python for grade XII - Sumita Arora
● Wikipedia