SQL programs
Updating a record,deleting a record ,group by city ,count of students, average of marks,order in descending order
First create a table to insert the values :
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sq = '''CREATE TABLE students(
id INTEGER PRIMARY KEY,
name TEXT ,
marks INTEGER,
city TEXT)'''
cursor.execute(sq)
print('table is created successfully')
con.commit()
con.close()
insert the values in the table:
import sqlite3
def muldata(dl):
con = sqlite3.connect('st2.db')
cursor = con.cursor()
iq = '''INSERT INTO students
VALUES(?,?,?,?) '''
cursor.executemany(iq, dl)
con.commit()
print("no of records inserted: ",cursor.rowcount)
con.commit()
con.close()
dl = [(1,'raj',92,'banglore'),(2,'raju',91,'chennai'),(3,'rajesh',93,'mumbai'),(4,'martha',94,'banglore'),(5,'ulrich',90,'chennai'),(6,'jonas',89,'mumbai'),(7,'anna',97,'banglore'),(8,'hanna',96,'chennai'),(9,'charlotte',87,'mumbai'),(10,'thomas',79,'banglore')]
muldata(dl)
print and see how the table looks initially:
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''SELECT * FROM students
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
con.commit()
con.close()
1) updating a record :
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''UPDATE students
SET name = 'mani'
WHERE id = 1
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
con.commit()
con.close()
2) deleting a record:
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''DELETE FROM students
WHERE name = 'mani'
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
con.commit()
con.close()
3) Group by city:
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''SELECT city
FROM students
GROUP BY city;
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
'''for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
'''
con.commit()
con.close()
4) marks in descending order:
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''SELECT marks
FROM students
ORDER BY marks DESC;
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
'''for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
'''
con.commit()
con.close()
5) Average of marks :
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''SELECT AVG(marks)
FROM students
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
'''for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
'''
con.commit()
con.close()
6)count of students:
import sqlite3
con = sqlite3.connect('st2.db')
cursor = con.cursor()
sqlit = '''SELECT count(id)
FROM students
'''
cursor.execute(sqlit)
records = cursor.fetchall()
print(records)
'''for row in records:
print('id:', row[0])
print('name:',row[1])
print('marks:',row[2])
'''
con.commit()
con.close()
Comments
Post a Comment