0% found this document useful (0 votes)
31 views2 pages

Python Function for Stationery Table

The document contains practice questions for establishing connectivity between Python and MySQL, focusing on various operations such as displaying records, inserting data, and updating records in different tables. It includes specific details about table structures and connectivity parameters for each scenario. The questions are aimed at helping users write Python programs to interact with MySQL databases effectively.

Uploaded by

pammikumaari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Python Function for Stationery Table

The document contains practice questions for establishing connectivity between Python and MySQL, focusing on various operations such as displaying records, inserting data, and updating records in different tables. It includes specific details about table structures and connectivity parameters for each scenario. The questions are aimed at helping users write Python programs to interact with MySQL databases effectively.

Uploaded by

pammikumaari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MYSQL-PYTHON CONNECTIVITY PRACTICE QUESTIONS

1. Sartaj has created a table named Student in MYSQL database, SCHOOL:


rno(Roll number )- integer
name(Name) - string
DOB (Date of birth) – Date
– float

Note the following to establish connectivity between Python and MySQL:


- root , - tiger , - localhost
Sartaj, now wants to display the records of students whose fee is more than 5000. Help Sartaj to write the
program in Python.

2. Kabir wants to write a program in Python to insert the following record in the table named Student in
MYSQL database, SCHOOL:
rno(Roll number )- integer
name(Name) - string
DOB (Date of birth) – Date
– float

Note the following to establish connectivity between Python and MySQL:


- root , - tiger , - localhost
The values of fields rno, name, DOB and fee has to be accepted from the user. Help Kabir to write the program
in Python.

3 A table, named STATIONERY, in ITEMDB database, has the following structure:

Field Type
itemNo int(11)
itemName varchar(15)
price float
qty int(11)
Write the following Python function to perform the specified operation:
AddAndDisplay(): To input details of an item and store it in the table STATIONERY. The function should
then retrieve and display all records from the STATIONERY table where the Price is greater than 120.
Assume the following for Python-Database connectivity: Host: localhost, User: root, Password: Pencil

4. MySQL database named WarehouseDB has a product_inventory table in MySQL which contains the
following attributes:
• Item_code: Item code (Integer)
• Product_name: Name of product (String)
• Quantity: Quantity of product (Integer)
• Cost: Cost of product (Integer)

Consider the following details to establish Python-MySQL connectivity:


• Username: admin_user , • Password: warehouse2024 , • Host: localhost

Write a Python program to change the Quantity of the product to 91 whose Item_code is 208 in the
product_inventory table.
5

Common questions

Powered by AI

A persistent database connection reduces the overhead of establishing a new connection for each database operation, thus improving performance in scenarios with frequent database access. However, it requires careful management of resources to avoid leaks. On the other hand, creating a new connection for each operation, while less efficient with high volumes, can simplify programming by avoiding issues related to stale or improperly closed connections. Persistent connections are more suitable for controlled environments, whereas transient connections offer safety in dealing with unexpected errors .

A Python program to add data to a MySQL table and then display specific records should follow these steps: Connect to the MySQL database with the required credentials (Host: localhost, User: root, Password: Pencil). Prompt the user for data input, then construct an SQL INSERT query using this data. Execute the insertion query and commit the transaction to save changes. For retrieval, frame a SELECT query with the specified condition, such as Price > 120, and execute it using the same or a new cursor object. Fetch all matching records and print them to the console .

Cursor objects serve as an interface to execute SQL commands and fetch data from the database. They provide methods like execute() for running SQL queries and fetchall() or fetchone() for retrieving query results. Proper management involves closing the cursor after use to release database resources. Ensuring cursors are closed properly is important, especially when utilizing multiple cursors, and can be handled using context managers or finally blocks to guarantee closure even in the event of an exception .

To design a Python program to display student records with a fee greater than 5000, the program should first establish a connection to the MySQL database using the provided credentials (Username: root, Password: tiger, Host: localhost). Then, it should execute a SELECT SQL query to retrieve records from the 'Student' table where the 'Fee' column has values greater than 5000. The steps include importing the MySQL.connector module, establishing connection with mysql.connector.connect(), executing the query with a cursor object, and finally fetching and displaying the results .

Python connects to different databases using specific libraries, like mysql.connector for MySQL and psycopg2 for PostgreSQL. The connection process generally involves specifying the host, username, password, and database name. While the basic structure of establishing a connection, executing queries, and handling exceptions is similar across databases, the specific library and API functions used (e.g., connect(), cursor(), execute()) depend on the database type. Support for different SQL syntax and data types also varies by database system .

To create a Python function that updates a record in a MySQL database based on a specified condition, first connect to the database with given credentials (Username: admin_user, Password: warehouse2024, Host: localhost). Next, prepare an update SQL query that includes the condition (e.g., updating the Quantity to 91 for Item_code 208 in the product_inventory table). Execute this prepared statement using a cursor object. After the execution, commit the transaction to save changes to the database. Finally, close both the cursor and the connection to the database to ensure resource cleanup .

Parameterized queries prevent SQL injection by separating SQL logic from the data inputs. In Python, this means using placeholders (like %s in mysql.connector or ? in SQLite) in the SQL statement and passing data inputs separately. This way, the database treats input data strictly as a string rather than executable SQL code, preventing attackers from injecting harmful SQL code via input fields. This method ensures that inputs do not alter the intended SQL logic of the operation .

Challenges include handling SQL injection, ensuring data type consistency, and managing database connections properly. To mitigate SQL injection risks, use parameterized queries instead of string interpolation when inserting user inputs into SQL statements. Validate and cast inputs to the appropriate data types to avoid type errors. Properly manage database connections by using try-except-finally blocks to handle potential connectivity issues and ensure that connections are closed correctly in the finally block to prevent resource leaks .

Secure database connections are crucial to protect sensitive data from unauthorized access and vulnerabilities such as SQL injection. Issues can arise if credentials are hard-coded or improperly secured, leading to password leaks. To enhance security, use environment variables or configuration files to store credentials, employ secure communication protocols like TLS/SSL for data transmission, and implement parameterized queries to safeguard against SQL injection. Regular security audits and updates to the database and server software can prevent exploitation of any known vulnerabilities .

When writing a Python function to insert data into a MySQL table, it is important to establish a database connection with the credentials provided (Username: root, Password: tiger, Host: localhost). User inputs for the fields should be collected using input() functions in Python. The program should parameterize the insertion query to prevent SQL injection attacks and correctly format the types (e.g., integers, strings, dates) for insertion into the database. After inserting, changes should be committed to the database using the connection's commit() method, and finally, the connection should be closed .

You might also like