PYTHON LESSON 28 OF 32

SQLite: your first database

SQLite stores tables in a database file or in memory. SQL SELECT reads rows. Always pass user values as parameters instead of formatting them into SQL text. Commit changes when using a persistent database.

हिन्दी में समझें

SQLite टेबल में डेटा रखता है। SELECT पंक्तियाँ पढ़ता है। उपयोगकर्ता के मान हमेशा पैरामीटर से दें; SQL टेक्स्ट में सीधे न जोड़ें। स्थायी डेटा के लिए commit करें।

अभ्यास: SELECT से पहले Ravi का score 70 डालें और दोनों पंक्तियाँ दिखाएँ।

Hinglish mein samjhein

SQLite tables mein data rakhta hai. SELECT rows padhta hai. User values ko parameters se pass karo, SQL string mein directly mat jodo. Persistent changes commit karo.

Practice: SELECT se pehle Ravi score70 insert karo aur dono rows dikhao.

A small Python example

import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE scores (name TEXT, score INTEGER)")
db.execute("INSERT INTO scores VALUES (?, ?)", ("Asha", 90))
print(db.execute("SELECT * FROM scores").fetchall())
db.close()

The browser playground runs real Python with Pyodide. Use sample variables instead of interactive input(). Browser package support differs from desktop Python.

Try this example in the Python playground →

Practise this idea

Insert Ravi with score 70 before the SELECT, then show both rows.

Check answer & explanation
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE scores (name TEXT, score INTEGER)")
db.execute("INSERT INTO scores VALUES (?, ?)", ("Asha", 90))
db.execute("INSERT INTO scores VALUES (?, ?)", ("Ravi", 70))
print(db.execute("SELECT * FROM scores").fetchall())
db.close()

Insert the extra parameterized row before SELECT and before closing the connection. fetchall() then includes both stored records.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

How should user values enter SQL?

  1. Through parameters
  2. By string concatenation
  3. As table names
Check the quiz answer

Through parameters

Parameters keep values separate from SQL instructions.

References & corrections

Use the official Python documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.