-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_task.py
More file actions
43 lines (31 loc) · 1.05 KB
/
sql_task.py
File metadata and controls
43 lines (31 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""sql_task.py
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1w8HPEO28aX6JRO7WpVPPI0ctVuswKCxg
"""
import sqlite3
conn = sqlite3.connect("students.db")
cursor = conn.cursor()
# Create table
cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
marks INTEGER
)
""")
# Insert sample data
cursor.execute("DELETE FROM students") # prevent duplicates
cursor.execute("INSERT INTO students (name, marks) VALUES ('Ravi', 85)")
cursor.execute("INSERT INTO students (name, marks) VALUES ('Priya', 92)")
cursor.execute("INSERT INTO students (name, marks) VALUES ('Kiran', 70)")
conn.commit()
print("Student Database Ready")
limit = int(input("Enter marks limit to find students below that: "))
cursor.execute("SELECT name, marks FROM students WHERE marks < ?", (limit,))
results = cursor.fetchall()
print(f"\nStudents who scored less than {limit}:")
for r in results:
print("Name:", r[0], "| Marks:", r[1])
conn.close()