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

Python નિશ્ચિત સાંકળમાં ત્રણ પદાર્થો દ્વારા SQLite સાથે વાત કરે છે: sqlite3.connect() database ની file ખોલે છે, connection નું cursor() તમને એક કામદાર આપે છે, અને cursor.execute() કોઈ પણ SQL ની string ચલાવે છે.

9 min read · 9 cards · 2 checks

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


Theory

Shell એ પ્રેક્ટિસ હતી; Python એ ઉત્પાદન છે

અત્યાર સુધીનું બધું sqlite3 ના shell માં થયું: તમે ટાઈપ કર્યું, shell એ જવાબ આપ્યો. તમારા માટે ઠીક, પરીક્ષા વિભાગ માટે નકામું: એમને ResultDesk એક program તરીકે જોઈએ છે: એક menu, બટન, જાતે બનતા reports.

Programs shells માં ટાઈપ કરી શકતાં નથી. તમારો Python નો code જાતે college.db ખોલે, જાતે SQL ચલાવે, અને જાતે જવાબો વાંચે.

Python એનો પુલ પોતાની standard library માં આપે છે: sqlite3 module. કોઈ installation નહીં, એક import, ત્રણ પદાર્થ.

Theory

ફોનની લાઈન, રિસેપ્શનિસ્ટ, વિનંતીની ચિઠ્ઠી

Program માંથી database સાથે વાત કરવાનાં ત્રણ સ્તર છે:

  • connect() ઓફિસને ફોન કરે છે: college.db સુધીની એક ખુલ્લી ફોનની લાઈન (Connection નો પદાર્થ).
  • cursor() એ લાઈન પર રિસેપ્શનિસ્ટ લાવે છે (Cursor નો પદાર્થ): જે ખરેખર વિનંતીઓ સંભાળે છે.
  • execute() રિસેપ્શનિસ્ટને એક વિનંતીની ચિઠ્ઠી આપે છે: તમારું SQL, Python ની string પર લખાયેલું.

લાઈન, રિસેપ્શનિસ્ટ, ચિઠ્ઠી. તમે ક્યારેય લખશો એ દરેક database નો program આ ત્રણથી શરૂ થાય છે.

Theory

પ્રમાણભૂત હાડપિંજર

પાંચ લીટી, એક એકમ તરીકે યાદ રાખવા લાયક:

1. import sqlite3

2. conn = sqlite3.connect('college.db'): file ખોલે છે, Connection આપે છે.

3. cur = conn.cursor(): Cursor આપે છે, SQL ચલાવનાર.

4. cur.execute('SELECT * FROM marks'): એક SQL નું વિધાન ચલાવે છે, string તરીકે અપાયેલું.

5. conn.close(): ફોન મૂકે છે, file છોડે છે.

આ એકમના બાકીના દરેક પાઠ આ હાડપિંજર પર ફક્ત માંસ ચઢાવે છે: પરિણામ લેવાં, ફેરફાર 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

ખાલી database નું રહસ્ય

એક student ની script કહે છે sqlite3.connect('collage.db') (જોડણીની ભૂલ) અને પછી marks માંથી SELECT કરે છે. ખરેખરી file college.db ત્યાં જ પડી છે, data થી ભરેલી. Tap કરતાં પહેલાં: કઈ ભૂલ દેખાય છે, અને folder માં હવે કઈ નવી વસ્તુ છે?

Show the answer

ભૂલ છે no such table: marks, અને folder માં હવે collage.db નામની તદ્દન નવી, ખાલી file છે.

connect() જોડણી તપાસતું નથી: ખૂટતી file ચૂપચાપ બની જાય છે. એટલે typos connect ના સમયે જોરથી નિષ્ફળ જતા નથી; એ query ના સમયે ગૂંચવણભરી રીતે નિષ્ફળ જાય છે, એવા database પર જેમાં કશું નથી. જે કોષ્ટક તમને ખાતરી હોય એના પર 'no such table' દેખાય, ત્યારે પહેલાં file નું નામ અને working directory તપાસો.

Quiz

એકમ 2 નાં shell નાં કૌશલ્યો ફરી વાપરવા એક student Python માં cur.execute('.dump marks') લખે છે. શું થાય છે?

  1. '.' પાસે syntax ની ભૂલ: dot-commands એ shell નાં લક્ષણ છે, SQL નહીં, અને execute() ફક્ત SQL સ્વીકારે છે
  2. એ ચાલે છે: shell જે સ્વીકારે એ બધું execute સ્વીકારે છે
  3. એ કોષ્ટકને પડદા પર dump કરે છે
  4. એ ચૂપચાપ marks.sql નામની file બનાવે છે
Show the answer

'.' પાસે syntax ની ભૂલ: dot-commands એ shell નાં લક્ષણ છે, SQL નહીં, અને execute() ફક્ત SQL સ્વીકારે છે

execute() બરાબર એક ભાષા બોલે છે: SQL. Dot-commands (.dump, .schema, .mode) sqlite3 ના command-line shell નાં છે, જે તદ્દન જુદો program છે: execute() ને અપાય તો એ બકવાસ છે અને ભૂલ આપે છે. Shell સામે module ની આ સીમા આખા એકમની સૌથી સામાન્ય ખ્યાલની લપસણ છે. (Program દ્વારા dump કરવાનું અસ્તિત્વમાં છે, connection ના iterdump() દ્વારા, જે પરીક્ષાના જવાબમાં એક લીટી લાયક છે.)

Watch out

હાડપિંજરની ત્રણ લપસણ

Cursor છોડી દેવો: આ અભ્યાસક્રમની પ્રમાણભૂત ભાતમાં હરોળ cursor દ્વારા આવે છે: conn → cursor → execute ને એક સાંકળ તરીકે શીખો.

દરેક execute() દીઠ એક વિધાન: એક જ call માં 'CREATE ...; INSERT ...' ખડકવાથી નિષ્ફળ જાય છે; scripts માટે executescript() છે.

close() એ save નથી: commit() વગર બંધ કરવાથી commit ન થયેલા ફેરફાર ફેંકાઈ જાય છે: બરાબર એ ફાંદો જેનું transactions ના પાઠે વચન આપ્યું હતું, જે આગલા પાઠમાં બરાબર નિષ્ક્રિય કરાય છે.

Theory

તમે જે જાણો છો એ બધું હમણાં જ દારૂગોળો બન્યું

નોંધો કે execute() શું લે છે: કોઈ પણ SQL ની string. એકમ 1 નું દરેક SELECT, JOIN, GROUP BY, CASE અને trigger હવે Python માંથી ચાલે છે, અકબંધ, ફક્ત આસપાસ અવતરણચિહ્નો સાથે. વિષયના બે અડધિયા મળી ગયા: SQL એ ભાષા છે, Python એ બોલનાર. આગલા પાઠમાં રિસેપ્શનિસ્ટ જવાબો પાછા આપવાનું શરૂ કરે છે: fetchone અને fetchall.

Summary

Key takeaways

  • import sqlite3: standard library, install કરવાનું કશું નહીં.
  • conn = sqlite3.connect('file.db') database ખોલે છે અને નામ નવું હોય તો એને બનાવે છે (typo નો ફાંદો).
  • cur = conn.cursor() કામદાર આપે છે; cur.execute('SQL') બરાબર એક વિધાન ચલાવે છે.
  • conn.close() file છોડે છે; બંધ કરવાથી બાકી રહેલા ફેરફાર સચવાતા નથી.
  • Dot-commands execute() ની અંદર ક્યારેય ચાલતાં નથી: ફક્ત SQL.
  • પાંચ લીટીનું હાડપિંજર (import, connect, cursor, execute, close) દરેક database ના program નો પાયો છે.
  • Memory hook: ફોનની લાઈન, રિસેપ્શનિસ્ટ, વિનંતીની ચિઠ્ઠી.

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