CoolFace
Apppublic

kavyabammidi/text2sql

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
sql.py48 linesDownload Raw Back to root
1import sqlite3
2
3# Connect to SQLite database (Creates 'student.db' if not exists)
4connection = sqlite3.connect("student.db")
5
6# Creating a cursor object
7cursor = connection.cursor()
8
9# SQL to create the STUDENT table
10table_info = '''
11    CREATE TABLE IF NOT EXISTS STUDENT (
12        NAME TEXT,
13        CLASS TEXT,
14        SECTION TEXT,
15        MARKS INT
16    )
17'''
18
19# Execute table creation
20cursor.execute(table_info)
21
22# Clear existing records in the STUDENT table
23cursor.execute("DELETE FROM STUDENT")
24
25# Insert records into STUDENT table
26cursor.executemany('''INSERT INTO STUDENT (NAME, CLASS, SECTION, MARKS) VALUES (?, ?, ?, ?)''', [
27    ('kavya', 'datascience', 'A', 80),
28    ('Sravya', 'datascience', 'C', 50),
29    ('bhavya', 'ML', 'A', 80),
30    ('havya', 'datascience', 'B', 83),
31    ('navya', 'AI', 'A', 19),
32    ('keerthi', 'datascience', 'B', 70),
33    ('divya', 'AI', 'C', 64),
34    ('lavanya', 'ML', 'A', 46)
35])
36
37# Commit changes
38connection.commit()
39
40# Fetch and display all records
41print('The inserted records are:')
42data = cursor.execute('SELECT * FROM STUDENT')
43
44for row in data:
45    print(row)
46
47# Close the database connection
48connection.close()