Professional Documents
Culture Documents
ML PGM
ML PGM
ML PGM
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
Program
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
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(
connection_obj.execute(
connection_obj.execute(
connection_obj.execute(
connection_obj.execute(
connection_obj.execute(
connection_obj.commit()
connection_obj.close()
import sqlite3
# Connecting to sqlite
# connection object
connection_obj = sqlite3.connect('geek.db')
# cursor object
cursor_obj = connection_obj.cursor()
cursor_obj.execute(statement)
output = cursor_obj.fetchall()
print(row)
connection_obj.commit()
connection_obj.close()
Output
Result
The query construct for retrieving relevant information from the table is executed.
9. Delete records from the table
Aim
Algorithm
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")
Actual data
Result
The python program to delete records from the table was executed successfully.
10. Update the values from the table
Aim
Algorithm
import sqlite3
connection = sqlite3.connect('my_database.db')
connection.close()
connection = sqlite3.connect('my_database.db')
connection.execute(
connection.execute(
connection.execute(
print("Actual data")
print(row)
print("After updating")
print(row)
connection.close()
Output
Actual data
After updating
Result
The python program to update the values from the table was executed.