Importing sqlite3 module: connect() and execute() methods

Python एक fixed chain में तीन objects के ज़रिए SQLite से बात करता है: sqlite3.connect() database file खोलता है, connection का cursor() आपको एक worker देता है, और cursor.execute() कोई भी SQL string चलाता है।

9 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Shell practice थी; Python product है

अब तक सब कुछ sqlite3 shell में हुआ: आपने type किया, shell ने जवाब दिया। आपके लिए ठीक, exam cell के लिए बेकार: वे चाहते हैं ResultDesk एक program हो: एक menu, buttons, reports जो ख़ुद generate हों।

Programs shells में type नहीं कर सकते। आपके Python code को college.db ख़ुद खोलनी होगी, SQL ख़ुद चलाना होगा, और जवाब ख़ुद पढ़ने होंगे।

Python अपनी standard library में यह bridge ship करता है: sqlite3 module। कोई installation नहीं, एक import, तीन objects।

Theory

Phone line, receptionist, request slip

एक program से database से बात करने की तीन layers हैं:

  • connect() office को dial करता है: college.db के लिए एक खुली phone line (Connection object)।
  • cursor() उस line पर एक receptionist लाता है (Cursor object): जो असल में requests process करता है।
  • execute() receptionist को एक request slip थमाता है: आपका SQL, एक Python string पर लिखा।

Line, receptionist, slip। आप जो भी database program कभी लिखेंगे वह इन तीनों से शुरू होता है।

Theory

Canonical skeleton

पाँच lines, एक unit के रूप में याद रखने लायक़:

1. import sqlite3

2. conn = sqlite3.connect('college.db'): file खोलता है, एक Connection return करता है।

3. cur = conn.cursor(): एक Cursor return करता है, SQL runner।

4. cur.execute('SELECT * FROM marks'): एक SQL statement चलाता है, एक string के रूप में दिया गया।

5. conn.close(): फोन रखता है, file release करता है।

इस unit का बाक़ी हर lesson इस skeleton पर बस मांस जोड़ता है: results fetch करना, changes commit करना।

Practical

ResultDesk का पहला database program

import sqlite3

# open (or create!) the database file
conn = sqlite3.connect('college.db')

# the worker that runs SQL
cur = conn.cursor()

# any SQL from Unit 1 travels as a plain string:
cur.execute("CREATE TABLE IF NOT EXISTS marks (roll INTEGER, subject TEXT, score INTEGER)")

cur.execute("SELECT roll, score FROM marks WHERE subject = 'DBMS'")
# (reading the rows: next lesson's fetchone/fetchall)

conn.close()   # always hang up

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Empty database का mystery

एक student की script कहती है sqlite3.connect('collage.db') (spelling slip) और फिर marks से SELECT करती है। असली file college.db बिल्कुल वहीं बैठी है, data से भरी। tap करने से पहले: कौन सा error दिखता है, और folder में अब नया क्या मौजूद है?

Show the answer

Error है no such table: marks, और folder में अब collage.db नाम की एक बिल्कुल नई, खाली file है।

connect() spelling check नहीं करता: एक missing file चुपचाप create हो जाती है। तो typos connect time पर ज़ोर से fail नहीं होते; वे query time पर confusingly fail होते हैं, एक ऐसे database पर जिसमें कुछ नहीं है। जब आप एक ऐसी table पर 'no such table' देखें जिसके मौजूद होने का आपको भरोसा है, पहले filename और working directory check कीजिए।

Quiz

Unit 2 की shell skills reuse करने के लिए, एक student Python में cur.execute('.dump marks') लिखता है। क्या होता है?

  1. '.' के पास Syntax error: dot-commands shell features हैं, SQL नहीं, और execute() सिर्फ़ SQL accept करता है
  2. यह काम करता है: execute shell जो भी accept करता है वह accept करता है
  3. यह table को screen पर dump कर देता है
  4. यह चुपचाप marks.sql नाम की एक file बना देता है
Show the answer

'.' के पास Syntax error: dot-commands shell features हैं, SQL नहीं, और execute() सिर्फ़ SQL accept करता है

execute() बिल्कुल ONE language बोलता है: SQL। Dot-commands (.dump, .schema, .mode) sqlite3 command-line shell के हैं, बिल्कुल अलग program: execute() को दिए जाने पर वे gibberish हैं और एक error उठाते हैं। यह shell-बनाम-module boundary पूरी unit का सबसे common conceptual slip है। (Programmatic dumping मौजूद है, connection के iterdump() के ज़रिए, exam answer में एक line लायक़।)

Watch out

तीन skeleton slips

Cursor skip करना: rows इस syllabus के canonical pattern में cursor के ज़रिए आते हैं: conn → cursor → execute को एक chain के रूप में सीखिए।

Prति execute() एक statement: एक call में 'CREATE ...; INSERT ...' stack करना fail होता है; scripts के लिए executescript() मौजूद है।

close() save नहीं है: commit() के बिना close करना uncommitted changes फेंक देता है: बिल्कुल वह trap जो transactions lesson ने promise किया, अगले lesson में सही तरीक़े से defused।

Theory

जो आप जानते हैं वह अभी ammunition बन गया

नोटिस कीजिए execute() क्या लेता है: कोई भी SQL string। Unit 1 का हर SELECT, JOIN, GROUP BY, CASE और trigger अब Python से चलता है, unchanged, इसके चारों तरफ़ quotes के साथ। Subject के दो हिस्से मिल गए हैं: SQL language है, Python speaker है। अगला lesson receptionist जवाब वापस देना शुरू करता है: fetchone और fetchall।

Summary

Key takeaways

  • import sqlite3: standard library, install करने के लिए कुछ नहीं।
  • conn = sqlite3.connect('file.db') database खोलता है और नाम नया होने पर इसे CREATE करता है (typo trap)।
  • cur = conn.cursor() worker देता है; cur.execute('SQL') बिल्कुल एक statement चलाता है।
  • conn.close() file release करता है; close करना pending changes save नहीं करता।
  • Dot-commands execute() के अंदर कभी काम नहीं करते: सिर्फ़ SQL।
  • Five-line skeleton (import, connect, cursor, execute, close) हर database program के नीचे है।
  • Memory hook: phone line, receptionist, request slip।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python interaction with SQLite

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Importing sqlite3 module: connect() and execute() methods · Database Handling using Python · Gri-Learn