ML PGM

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

8.

python program to construct query for retrieving relevant information from the
table python MySQL

Aim

To write python program to construct query for retrieving relevant information from the
table python MySQL

Algorithm

Step 1: import sqlite3


Step 2: Use the connection object to invoke the cursor method and create a cursor object.
Step 3: Define the query and save it to a variable.
Step 4: Use the cursor object to invoke the execute method and run the query.
Step 5: Close the database connection

Program

import sqlite3

# Connecting to sqlite

# connection object

connection_obj = sqlite3.connect('geek.db')

# cursor object

cursor_obj = connection_obj.cursor()

connection_obj.execute("""CREATE TABLE SAMPLE(

Email varchar(255),

Name varchar(50),

Score int

);""")

connection_obj.execute(
"""INSERT INTO SAMPLE (Email,Name,Score) VALUES
("sss@gmail.com","sss1",25)""")

connection_obj.execute(

"""INSERT INTO SAMPLE (Email,Name,Score) VALUES


("sss2@gmail.com","sss2",15)""")

connection_obj.execute(

"""INSERT INTO SAMPLE (Email,Name,Score) VALUES


("sss3@gmail.com","sss3",36)""")

connection_obj.execute(

"""INSERT INTO SAMPLE (Email,Name,Score) VALUES


("sss4@gmail.com","sss4",27)""")

connection_obj.execute(

"""INSERT INTO SAMPLE (Email,Name,Score) VALUES


("sss5@gmail.com","sss5",40)""")

connection_obj.execute(

"""INSERT INTO SAMPLE (Email,Name,Score) VALUES


("sss6@gmail.com","sss6",36)""")

connection_obj.execute(

"""INSERT INTO SAMPLE (Email,Name,Score) VALUES


("sss7@gmail.com","sss7",27)""")

connection_obj.commit()

# Close the connection

connection_obj.close()

import sqlite3

# Connecting to sqlite

# connection object

connection_obj = sqlite3.connect('geek.db')

# cursor object
cursor_obj = connection_obj.cursor()

# to select all column we will use

statement = '''SELECT * FROM SAMPLE'''

cursor_obj.execute(statement)

print("All the data")

output = cursor_obj.fetchall()

for row in output:

print(row)

connection_obj.commit()

# Close the connection

connection_obj.close()
Output

All the data

('sss@gmail.com', 'sss1', 25)

('sss2@gmail.com', 'sss2', 15)

('sss3@gmail.com', 'sss3', 36)

('sss4@gmail.com', 'sss4', 27)

('sss5@gmail.com', 'sss5', 40)

('sss6@gmail.com', 'sss6', 36)

('sss7@gmail.com', 'sss7', 27)

Result

The query construct for retrieving relevant information from the table is executed.
9. Delete records from the table

Aim

To write a python program to delete records from the table

Algorithm

Step 1: Import sqlite3 package.

Step 2: Create a connection object using the connect() method by passing the name of the
database as a parameter to it.
Step 3: The cursor() method returns a cursor object using which the program communicate with
SQLite3 .

Program

import sqlite3
connection = sqlite3.connect('my_database.db')
connection.execute('''CREATE TABLE ship5 (ship_id INT, ship_name \
TEXT NOT NULL, ship_destination CHAR(50) NOT NULL); ''')
print("Ship table created successfully")
connection.close()
connection = sqlite3.connect('my_database.db')
connection.execute("INSERT INTO ship5 VALUES (1, 'tata-hitachi','noida' )")
connection.execute("INSERT INTO ship5 VALUES (2, 'tata-mumbai','mumbai' )")
connection.execute("INSERT INTO ship5 VALUES (3, 'tata-express','hyderabad' )")
cursor = connection.execute("SELECT * from ship5")
print("Actual data")
for row in cursor:
print(row)
connection.execute("DELETE from ship5 where ship_id=2")

print("After deleting ship id = 2 row")


cursor = connection.execute("SELECT * from ship5")
for row in cursor:
print(row)
connection.close()
Output

Ship table created successfully

Actual data

(1, 'tata-hitachi', 'noida')

(2, 'tata-mumbai', 'mumbai')

(3, 'tata-express', 'hyderabad')

After deleting ship id = 2 row

(1, 'tata-hitachi', 'noida')

(3, 'tata-express', 'hyderabad')

Result

The python program to delete records from the table was executed successfully.
10. Update the values from the table

Aim

To write a python program to update the values from the table.

Algorithm

Step 1: import mysql. connector package.


Step 2: Create a connection object using the mysql. connector. ...
Step 3: Create a cursor object by invoking the cursor() method on the connection object created
above.
Step 4: Then, execute the UPDATE statement by passing it as a parameter to the execute()
method.
Program

import sqlite3

connection = sqlite3.connect('my_database.db')

connection.execute('''CREATE TABLE EMPLOYEE(FIRST_NAME VARCHAR(255),

LAST_NAME VARCHAR(255),AGE int, SEX VARCHAR(255), INCOME int) ''')

print("Employee table created successfully")

connection.close()

connection = sqlite3.connect('my_database.db')

connection.execute(

'''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)

VALUES ('Mukesh', 'Sharma', 20, 'M', 9000)''')

connection.execute(

'''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)

VALUES ('Ankit', 'Pandey', 24, 'M', 6300)''')

connection.execute(

'''INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME)

VALUES ('Tanu', 'Mishra', 24, 'F', 6500)''')


cursor = connection.execute("SELECT * from EMPLOYEE")

print("Actual data")

for row in cursor:

print(row)

connection.execute('''UPDATE EMPLOYEE SET INCOME = 5000 WHERE Age<25;''')

print("After updating")

cursor = connection.execute("SELECT * from EMPLOYEE")

for row in cursor:

print(row)

connection.close()

Output

Employee table created successfully

Actual data

('Mukesh', 'Sharma', 20, 'M', 9000)

('Ankit', 'Pandey', 24, 'M', 6300)

('Tanu', 'Mishra', 24, 'F', 6500)

After updating

('Mukesh', 'Sharma', 20, 'M', 5000)

('Ankit', 'Pandey', 24, 'M', 5000)

('Tanu', 'Mishra', 24, 'F', 5000)

Result

The python program to update the values from the table was executed.

You might also like