name = "ThulasiDhasan" map = {} for ch in name: map[ch] = map.get(ch, 0) + 1 print(map)
2. Flatten list of the list in Python
lst = [[2, 3, 5], [6, 7, 8], [9, 0, 1]] t_lst = [ele for sublist in lst for ele in sublist] print(t_lst)
name = "ThulasiDhasan" map = {} for ch in name: map[ch] = map.get(ch, 0) + 1 print(map)
lst = [[2, 3, 5], [6, 7, 8], [9, 0, 1]] t_lst = [ele for sublist in lst for ele in sublist] print(t_lst)
from collections import namedtuple Contact = namedtuple("Contact", ["cid","name", "email", "mobile"])
mysql = { 'host': 'localhost', 'user': 'root', 'passwd': 'password', 'db': 'counselor_db'}
import pymysql import config class DbUtil: @staticmethod def get_connection(): try: con = pymysql.connect(config.mysql['host'], config.mysql['user'],config.mysql['passwd'],config.mysql['db']) except pymysql.MySQLError as error: print('Exception number: {}, value {s}'.format(error.args[0], error)) raise else: return con @staticmethod def close_connection(conn): try: conn.close() except pymysql.MySQLError as error: print("While closing connection ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) raise @staticmethod def close_cursor(cursor): try: cursor.close() except pymysql.MySQLError as error: print("While closing the cursor ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) raise
import config from util import DbUtil import pymysql from contact import Contact class ContactDbOperations: def add_contact(self, new_contact): try: connection = DbUtil.get_connection() with connection.cursor() as cursor: cursor.execute("insert into contact(name,email,mobile) values(%s,%s,%s)", (new_contact.name , new_contact.email , new_contact.mobile)) connection.commit() except pymysql.MySQLError as error: print("While inserting Data ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) def get_contact(self, cid): try: conn = DbUtil.get_connection() cursor = conn.cursor() cursor.execute("select cid, name, email, mobile from contact where cid = %s", cid) contact = Contact(*cursor.fetchone()) return contact except pymysql.DatabaseError as error: print("While getting data from DB using id... ") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(conn) DbUtil.close_cursor(cursor) def update_contact(self, contact): try: conn = DbUtil.get_connection() cursor = conn.cursor() cursor.execute("update contact set name = %s ,email = %s, mobile = %s where cid = %s", contact) conn.commit() except pymysql.DatabaseError as error: print("While updating ... ") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(conn) DbUtil.close_cursor(cursor) def get_all_contacts(self): try: connection = DbUtil.get_connection() with connection.cursor() as cursor: cursor.execute("select cid, name, email, mobile from contact") rows = cursor.fetchall() contacts = self.get_list_data(rows) return contacts except Exception as error: print("While retrieving data ... ") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: if connection: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) def search_contact(self, search_str): try: connection = DbUtil.get_connection() with connection.cursor() as cursor: cursor.execute("select cid, name, email, mobile from contact where name like %s ", ('%' + search_str + '%')) rows = cursor.fetchall() contacts = self.get_list_data(rows) return contacts except Exception as error: print("While searching Data ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) @staticmethod def delete_contact(cid): try: connection = DbUtil.get_connection() cursor = connection.cursor() cursor.execute("delete from contact where cid = %s ", cid) connection.commit() except pymysql.DatabaseError as error: print("While deleting data ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) @staticmethod def get_list_data(rows): return [Contact(*row) for row in rows]
from dboperations import ContactDbOperations from contact import Contact from beautifultable import BeautifulTable class ContactService: def __init__(self): self.cs = ContactDbOperations() def add_contact(self): name = input("Name : ") email = input("Email : ") mobile = input("Mobile : ") contact = Contact(0, name, email, mobile) self.cs.add_contact(contact) self.show_all_contacts() def show_all_contacts(self): contacts = self.cs.get_all_contacts() ContactService.paint_data_list(contacts) def search_contact(self): name = input("Enter search name :") contacts = self.cs.search_contact(name) if contacts: ContactService.paint_data_list(contacts) else: print("No data found with search name :{}".format(name)) @staticmethod def paint_data(data): table = BeautifulTable() table.column_headers = ["CID", "NAME", "EMAIL", "MOBILE"] table.append_row([data.cid, data.name, data.email, data.mobile]) print(table) @staticmethod def paint_data_list(data): table = BeautifulTable() table.column_headers = ["CID", "NAME", "EMAIL", "MOBILE"] for li in data: table.append_row([li.cid, li.name, li.email, li.mobile]) print(table)
if __name__ == '__main__': cs = ContactService() while True: print("*"*40) print("1. Add Contact 2. View All 3.Search 4. Exit") print("*"*40) choice = int(input("Enter your choice :")) if choice == 1: cs.add_contact() elif choice == 2: cs.show_all_contacts() elif choice == 3: cs.search_contact() elif choice == 4: exit() else: print("Enter only 1 to 4 ......")
with open("outputfile.txt","w") as fw,open("inputfile.txt") as f: for line in f: fw.write(line)
Contact Book APP:
This sample application is to demonstrate CRUD operations using Python and MySQL.
install pymysql from the following link:
http://pymysql.readthedocs.io/en/latest/user/installation.html
--------------------------------------------------------------------------------------
from collections import namedtuple import pymysql import db.config as config from beautifultable import BeautifulTable Contact = namedtuple("Contact", ["cid","name", "email", "mobile"]) class DbUtil: @staticmethod def get_connection(): try: con = pymysql.connect(config.mysql['host'], config.mysql['user'],config.mysql['passwd'],config.mysql['db']) except pymysql.MySQLError as error: print('Exception number: {}, value {!r}'.format(error.args[0], error)) raise else: return con @staticmethod def close_connection(conn): try: conn.close() except pymysql.MySQLError as error: print("While closing connection ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) raise @staticmethod def close_cursor(cursor): try: cursor.close() except pymysql.MySQLError as error: print("While closing the cursor ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) raise class ContactService: def add_contact(self, new_contact): try: connection = DbUtil.get_connection() with connection.cursor() as cursor: cursor.execute("insert into contact(name,email,mobile) values(%s,%s,%s)", (new_contact.name , new_contact.email , new_contact.mobile)) connection.commit() except pymysql.MySQLError as error: print("While inserting Data ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) def get_contact(self, cid): try: conn = DbUtil.get_connection() cursor = conn.cursor() cursor.execute("select cid, name, email, mobile from contact where cid = %s", cid) contact = Contact(*cursor.fetchone()) self.paint_data(contact) except pymysql.DatabaseError as error: print("While getting data from DB using id... ") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(conn) DbUtil.close_cursor(cursor) def update_contact(self, contact): try: conn = DbUtil.get_connection() cursor = conn.cursor() cursor.execute("update contact set name = %s ,email = %s, mobile = %s where cid = %s", contact) conn.commit() except pymysql.DatabaseError as error: print("While updating ... ") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(conn) DbUtil.close_cursor(cursor) def show_contacts(self): try: connection = DbUtil.get_connection() with connection.cursor() as cursor: cursor.execute("select cid, name, email, mobile from contact") rows = cursor.fetchall() contacts = self.get_list_data(rows) self.paint_data_list(contacts) except Exception as error: print("While retrieving data ... ") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: if connection: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) def search_contact(self, search_str): try: connection = DbUtil.get_connection() with connection.cursor() as cursor: cursor.execute("select cid, name, email, mobile from contact where name like %s ", ('%' + search_str + '%')) rows = cursor.fetchall() contacts = self.get_list_data(rows) print(contacts) self.paint_data_list(contacts) except Exception as error: print("While searching Data ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) def delete_contact(self, cid): try: connection = DbUtil.get_connection() cursor = connection.cursor() cursor.execute("delete from contact where cid = %s ", cid) connection.commit() except pymysql.DatabaseError as error: print("While deleting data ...") print('Exception number: {}, value {!r}'.format(error.args[0], error)) finally: DbUtil.close_connection(connection) DbUtil.close_cursor(cursor) def get_list_data(self, rows): return [Contact(*row) for row in rows] def paint_data(self, data): table = BeautifulTable() table.column_headers = ["CID", "NAME", "EMAIL", "MOBILE"] table.append_row([data.cid, data.name, data.email, data.mobile]) print(table) def paint_data_list(self, data): table = BeautifulTable() table.column_headers = ["CID", "NAME", "EMAIL", "MOBILE"] for li in data: table.append_row([li.cid, li.name, li.email, li.mobile]) print(table)
Create the following 3 tables. Identify the primary
key(s) and foreign key(s) as appropriate.
Table-1
CREATE TABLE DEPT
(DEPTNO INT,
DNAME VARCHAR(14),
LOC VARCHAR(13) );
Insert the following data into the DEPT table (data is in the same sequence as the fields defined above):
10, 'ACCOUNTING', 'NEW YORK'
20, 'RESEARCH', 'DALLAS'
30, 'SALES', 'CHICAGO'
40, 'OPERATIONS', 'BOSTON'
Table-2
CREATE TABLE EMP(EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR INT,
HIREDATE DATE,
SAL FLOAT,
COMM FLOAT,
DEPTNO INT REFERENCES DEPT(DEPTNO));
Insert the following
data into the EMP table (data is in the same sequence as the fields defined
above):
7369,
'SMITH', 'CLERK', 7902, '17-DEC-1980', 800,
NULL, 20
7499,
'ALLEN', 'SALESMAN', 7698, '20-FEB-1981', 1600, 300, 30
7521,
'WARD', 'SALESMAN', 7698, '22-FEB-1981', 1250, 500, 30
7566,
'JONES', 'MANAGER', 7839, '2-APR-1981', 2975,
NULL, 20
7654,
'MARTIN', 'SALESMAN', 7698, '28-SEP-1981', 1250, 1400, 30
7698,
'BLAKE', 'MANAGER', 7839, '1-MAY-1981', 2850, NULL, 30
7782,
'CLARK', 'MANAGER', 7839, '9-JUN-1981', 2450, NULL, 10
7788,
'SCOTT', 'ANALYST', 7566, '09-DEC-1982', 3000, NULL, 20
7839,
'KING', 'PRESIDENT', NULL,
'17-NOV-1981', 5000, NULL, 10
7844,
'TURNER', 'SALESMAN', 7698,
'8-SEP-1981', 1500, NULL, 30
7876,
'ADAMS', 'CLERK', 7788, '12-JAN-1983', 1100, NULL, 20
7900,
'JAMES', 'CLERK', 7698, '3-DEC-1981', 950, NULL,
30
7902,
'FORD', 'ANALYST', 7566, '3-DEC-1981', 3000,
NULL, 20
7934,
'MILLER', 'CLERK', 7782,
'23-JAN-1982', 1300, NULL, 10
Table-3
CREATE
TABLE SALGRADE
(GRADE INT,
LOSAL INT,
HISAL INT);
Insert the following
data into the SALGRADE table (data is in the same sequence as the fields
defined above):
2, 1201,
1400
3, 1401,
2000
4, 2001,
3000
5, 3001,
9999
Practice Questions:
1.
Write
a query to display all the details of the employees who are working for
department 10 or 20.
2.
Write a query to display the names
of the employees whose salaries are 5000 or 3000 or 800. Write the query in 2
different ways.
3.
Write a query to display all the
details of the employees whose names are:
‘SCOTT’, ’BLAKE’, ’ADAMS’.
4.
Write a query to display the
employees whose date of hire is one of the following: ’Dec 17, 1981’, ’3rd
Dec, 1981’, ’21st Jan, 1981’.
5.
Write a query to display the details
of the employees who are getting paid in the range 2000 – 5000. Write the query
in two different ways.
6.
Write a query to display the details
of all the employees who joined during the period ’Jan 01st, 1981’
and ’31st Aug, 1981’.
7.
Write a query to display all the
details of the employees whose name begins with the letter ‘S’.
8.
Write a query to display all the
details of the employees whose name is at least 4 characters long.
9.
Write a query to display names of
the employees whose name contains the letter 'A'.
10.
Write a query to display names of
the employees who have joined in the month where the name of the month starts
with the letter 'A'.
11.
Write a query to display names of
the employees whose date of joining is within the first 9 days of any calendar month.
12.
Write a query to display the names
of the employees who are not receiving any commission.
13.
Write a query to display the names
of the employees whose job title is ‘MANAGER’ and who are working for department
20.
14.
Write a query to display the details
of employees, whose name starts with letter ‘J’ and whose salary is greater
than 2500 and who work in the department 20.
15.
Write a query to display the details
of employees, whose name starts with letter ‘J’ or whose salary is greater than
2500 or who work in the department 20.
16.
Write a query to display the details
of all the employees whose job title is ‘MANAGER’ or ‘CLERK’.
17.
Write a query to display the
employees whose salary is same as that of the employees ‘FORD’ or ‘JAMES’.
18.
Write a query to display the details
of the employees who are working in the 'ACCOUNTING' department.
19.
Write a query to display the details
of all the salesmen belonging to the sales department who are receiving
commission.
20.
Write a query to display details of
the all the managers having experience not less than 20 years and working in
the location 'NEW YORK' or 'DALLAS' and working under some other manager.
21.
Write a query to display the salary
grade of the employee 'FORD'.
22.
Display the details of Managers
belonging to grades 4 or 5.
23.
Display the following data for all
the employees: Employee number, employee name, salary, grade, work location.
24.
Display the following data for all
the employees of the “Accounting” department: Employee number, employee name,
salary, work location.
25.
Display all grade 4 and grade 5
MANAGERS of the following departments: ACCOUNTING, SALES.
26.
Write a query to list the details of
all the employees who belong to the department where 'ADAMS' is working.
27.
Display the details of all employees
those who are senior (in terms of years of experience) to their own manager.
28.
Display the details of all the
departments – if the department has any employee then display the employee's
number and name as well.
29.
Display the details of employees
whose department is located in 'NEW YORK' city.
30.
Display the details of employees
whose salary is more than the salary of the employee ‘JAMES’.
31.
Display the details of all the
employees whose job title is same as that of the employee ‘JAMES’.
32.
Display the details of all the
employees who are senior to ‘JAMES’ (in terms of years of experience).
33. Display the details of all the employees who are working in the same department as that of ‘SCOTT’; do not include the details of ‘SCOTT’.
34. Display the details of the employees whose job titles are same as that of the employees of the ‘SALES’ department.
35. Display the details of all grade 3 employees whose department is same as that of the employee ‘SCOTT’
36. Display the details of all the employees working in the city of ‘NEW YORK’ or ‘DALLAS’ and whose job title is same as that of any grade 2 employee; however do not include the job title ‘SALESMAN’.
37. Display the details of all the employees whose salary is more than that of employee ‘SMITH’ but less than that of employee ‘BLAKE’; however, it should not be equal to that of ‘SCOTT’ or that of ‘FORD’.
38. Display the details of all employees to whom other people are reporting.
39. Display the details of all the employees who are working in the department where the manager of employee ‘BLAKE’ is working.
40. Display the following details of all the employees: employee number, name, salary, department name, location. If the employee is not working in any department then display just the employee number, name, salary.
41. Display the details of employees whose salaries are more than the salary of any employee belonging to department 10.
42. Display the details of all the employees working in department 10 whose salary is more than the highest salary being given in department 20.
43. Display the details of the nth highest paid employee.
44. Display the details of the highest paid employee from each department.
45. Display the details of the senior-most employee of each department.
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...