{"type": "session", "harness": "opencode", "id": "appdev-0075", "name": "database app"} {"type": "message", "message": {"role": "system", "content": "You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies."}} {"type": "message", "message": {"role": "user", "content": "Walk me through building a database app: key concepts, concrete details and common pitfalls. Include a runnable code example with its expected output."}} {"type": "message", "message": {"role": "assistant", "content": "**Full-Stack SQLite App (contacts manager):**\n\n```python\nimport sqlite3\nfrom fastapi import FastAPI, HTTPException\nfrom pydantic import BaseModel\nfrom typing import Optional\n\napp = FastAPI()\n\ndef init_db():\n conn = sqlite3.connect('contacts.db')\n conn.execute('''CREATE TABLE IF NOT EXISTS contacts (\n id INTEGER PRIMARY KEY AUTOINCREMENT,\n name TEXT NOT NULL,\n email TEXT UNIQUE,\n phone TEXT\n )''')\n conn.commit()\n conn.close()\n\ninit_db()\n\nclass Contact(BaseModel):\n name: str\n email: str\n phone: Optional[str] = None\n\n@app.get('/contacts')\ndef list_contacts():\n conn = sqlite3.connect('contacts.db')\n rows = conn.execute('SELECT * FROM contacts').fetchall()\n conn.close()\n return [{'id': r[0], 'name': r[1], 'email': r[2], 'phone': r[3]} for r in rows]\n\n@app.post('/contacts', status_code=201)\ndef create_contact(c: Contact):\n conn = sqlite3.connect('contacts.db')\n try:\n conn.execute('INSERT INTO contacts (name, email, phone) VALUES (?, ?, ?)',\n (c.name, c.email, c.phone))\n conn.commit()\n id = conn.execute('SELECT last_insert_rowid()').fetchone()[0]\n except sqlite3.IntegrityError:\n raise HTTPException(400, 'Email already exists')\n finally:\n conn.close()\n return {'id': id, **c.model_dump()}\n\n@app.get('/contacts/{id}')\ndef get_contact(id: int):\n conn = sqlite3.connect('contacts.db')\n row = conn.execute('SELECT * FROM contacts WHERE id = ?', (id,)).fetchone()\n conn.close()\n if not row:\n raise HTTPException(404, 'Contact not found')\n return {'id': row[0], 'name': row[1], 'email': row[2], 'phone': row[3]}\n\n@app.delete('/contacts/{id}')\ndef delete_contact(id: int):\n conn = sqlite3.connect('contacts.db')\n conn.execute('DELETE FROM contacts WHERE id = ?', (id,))\n conn.commit()\n conn.close()\n return {'deleted': id}\n```\n\n**Testing:**\n```bash\n# Create\ncurl -X POST http://localhost:8000/contacts \\\n -H 'Content-Type: application/json' \\\n -d '{\"name\": \"Alice\", \"email\": \"alice@example.com\"}'\n\n# List\ncurl http://localhost:8000/contacts\n```"}}