Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share code      Blog   Popular   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear
import sqlite3 # Connect to the SQLite database (or create it) conn = sqlite3.connect('hospital.db') cursor = conn.cursor() # Create the Patient table if it doesn't exist cursor.execute(''' CREATE TABLE IF NOT EXISTS Patient ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, issue TEXT, age INTEGER ) ''') # Functions to insert, delete, update patients def insert_patient(name, issue, age): cursor.execute('INSERT INTO Patient (name, issue, age) VALUES (?, ?, ?)', (name, issue, age)) conn.commit() def delete_patient(patient_id): cursor.execute('DELETE FROM Patient WHERE id = ?', (patient_id,)) conn.commit() def update_patient(patient_id, name=None, issue=None, age=None): row = cursor.execute('SELECT * FROM Patient WHERE id=?', (patient_id,)).fetchone() if not row: print("Patient not found.") return new_name = name if name is not None else row[1] new_issue = issue if issue is not None else row[2] new_age = age if age is not None else row[3] cursor.execute('UPDATE Patient SET name=?, issue=?, age=? WHERE id=?', (new_name, new_issue, new_age, patient_id)) conn.commit() # Example usage insert_patient('Ali', 'Fever', 30) insert_patient('Sara', 'Headache', 25) update_patient(1, age=31) delete_patient(2) # Show all patients for row in cursor.execute('SELECT * FROM Patient'): print(row) conn.close()

Stuck with a problem? Got Error? Ask ChatGPT!