{"library":"sqlmodel","code":"from typing import Optional\nfrom sqlmodel import Field, Session, SQLModel, create_engine, select\n\nclass Hero(SQLModel, table=True):\n    id: Optional[int] = Field(default=None, primary_key=True)\n    name: str\n    secret_name: str\n    age: Optional[int] = Field(default=None, index=True)\n\nsqlite_file_name = \"database.db\"\nsqlite_url = f\"sqlite:///{sqlite_file_name}\"\n\n# Or for in-memory:\n# sqlite_url = \"sqlite://\"\n\nengine = create_engine(sqlite_url, echo=True)\n\ndef create_db_and_tables():\n    SQLModel.metadata.create_all(engine)\n\ndef create_heroes():\n    hero_1 = Hero(name=\"Deadpond\", secret_name=\"Dive Wilson\")\n    hero_2 = Hero(name=\"Spider-Boy\", secret_name=\"Pedro Parqueador\", age=16)\n    hero_3 = Hero(name=\"Rusty-Man\", secret_name=\"Tommy Sharp\", age=48)\n\n    with Session(engine) as session:\n        session.add(hero_1)\n        session.add(hero_2)\n        session.add(hero_3)\n\n        session.commit()\n\n        session.refresh(hero_1)\n        session.refresh(hero_2)\n        session.refresh(hero_3)\n\n        print(\"Created heroes:\", hero_1, hero_2, hero_3)\n\ndef select_heroes():\n    with Session(engine) as session:\n        statement = select(Hero).where(Hero.age >= 18)\n        results = session.exec(statement)\n        heroes = results.all()\n        print(\"Adult heroes:\", heroes)\n\ndef main():\n    create_db_and_tables()\n    create_heroes()\n    select_heroes()\n\nif __name__ == \"__main__\":\n    main()","lang":"python","description":"This quickstart demonstrates how to define a `SQLModel` table, create a database engine (using SQLite in this case), create the database tables, and then insert and query data. It showcases the combined power of Pydantic-like model definition with SQLAlchemy's ORM operations. The `echo=True` in `create_engine` will print SQL statements to the console.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}