Created
July 8, 2026 15:39
-
-
Save Cecilia-Grace/f16d150f2e02023b5c6e3140df2b6e60 to your computer and use it in GitHub Desktop.
Selecting data with SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlite3 | |
| conn = sqlite3.connect('school.db') | |
| cursor = conn.cursor() | |
| # cursor.execute(''' | |
| # CREATE TABLE IF NOT EXISTS Students( | |
| # id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| # name TEXT, | |
| # age INTEGER | |
| # ) | |
| # ''') | |
| # name = input("Name: ") | |
| # age = int(input("Age: ")) | |
| # phone_number = input("Phone number: ") | |
| # cursor.execute('INSERT INTO Students(name, age, phone_number) VALUES(?, ?, ?)', (name, age, phone_number)) | |
| '''selecting all columns''' | |
| # query = 'SELECT * FROM Students' | |
| '''selecting specific column(s)''' | |
| # query = 'SELECT name FROM Students' | |
| '''rename column title''' | |
| # query = 'SELECT name AS student_name FROM Students' | |
| '''using case''' | |
| # query = ''' | |
| # SELECT name, age, | |
| # CASE | |
| # WHEN age < 13 THEN 'Toddler' | |
| # ELSE 'Adult' | |
| # END AS age_status | |
| # FROM Students; | |
| # ''' | |
| '''get upper case,length, substr,concantenate operator() to add a character(||)''' | |
| # query = ''' | |
| # SELECT name || lastname AS first_initial | |
| # FROM Students; | |
| # ''' | |
| '''mathematical operations(round, )''' | |
| # query = ''' | |
| # SELECT round(grade) AS rounded_grade | |
| # FROM Students; | |
| # ''' | |
| cursor.execute(query) | |
| students_data = cursor.fetchall() | |
| print(students_data) | |
| # conn.commit() | |
| conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment