Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C31-L1082-260698F1

Home Dashboard
Chapter 1 · Python Database Fundamentals

Python Database Programming & DB-API Basics

Priority
  • JobHigh
  • InterviewHigh
  • DevelopmentHigh
2,594
words
15
min read
2
tools

1. Learning Outcomes#

या chapter नंतर तुम्हाला:

  • Python application SQL database शी कशी communicate करते ते समजेल.
  • database driver म्हणजे काय आणि त्याची गरज का असते ते समजेल.
  • Python DB-API म्हणजे specification आहे, database किंवा driver नाही, हे स्पष्ट होईल.
  • connect() ची मुख्य भूमिका समजेल.
  • Connection object आणि Cursor object वेगवेगळे काय काम करतात ते समजेल.
  • basic database operation lifecycle step-by-step समजेल.
  • एका छोट्या Python example मधून query database पर्यंत कशी पोहोचते आणि result परत कसा येतो ते पाहता येईल.
  • Connection आणि Cursor बद्दलच्या common beginner confusions ओळखता येतील.

2. Python application ला database ची गरज का पडते?#

समजा आपण student management application तयार करत आहोत.

Program चालू असताना Python variables मध्ये data ठेवता येतो. पण program बंद झाला तरी data कायमचा ठेवायचा असेल, search करायचा असेल, अनेक records manage करायचे असतील किंवा वेगवेगळ्या users ना तोच data वापरायचा असेल, तर database उपयोगी पडतो.

उदाहरणार्थ application ला खालील कामे करावी लागू शकतात:

  • नवीन student record save करणे
  • student ची माहिती शोधणे
  • fee status update करणे
  • inactive record delete करणे
  • report साठी अनेक rows वाचणे

Python application ला ही कामे स्वतः database file किंवा database server च्या internal format मध्ये करायची नसतात.

मध्ये एक software layer काम करते.

त्या layer ला आपण database driver म्हणतो.

3. Python application आणि SQL database यांचा flow#

हा basic mental model लक्षात ठेवा:

Visual Diagram80%

Rendering diagram…

Flow डावीकडून सुरू होतो.

Python application एखादी database operation मागते. Driver ती request database ला समजेल अशा पद्धतीने पाठवतो. Database operation execute करते. मग result किंवा status driver मार्फत परत Python application पर्यंत येतो.

Key Idea

Python code आणि database यांच्यामध्ये database driver हा communication bridge म्हणून काम करतो.

4. Database driver म्हणजे काय?#

Database driver म्हणजे Python application आणि specific database system यांच्यामध्ये communication करणारे software/library.

उदाहरणार्थ SQLite, PostgreSQL, MySQL किंवा SQL Server यांच्याशी काम करताना वेगवेगळे drivers वापरले जाऊ शकतात.

काही Python driver/module examples:

  • sqlite3 — SQLite साठी Python standard library module
  • psycopg — PostgreSQL ecosystem मध्ये वापरला जाणारा driver
  • mysql.connector — MySQL साठी एक driver interface
  • pyodbc — ODBC-compatible databases शी काम करण्यासाठी

Driver चे काम फक्त SQL string पुढे पाठवणे एवढेच नाही.

तो सामान्यतः:

  • database connection तयार करण्यात मदत करतो
  • Python values database ला योग्य form मध्ये पाठवतो
  • SQL statements execute करण्यासाठी interface देतो
  • database कडून आलेले results Python objects मध्ये उपलब्ध करून देतो
  • database errors Python exceptions च्या स्वरूपात देतो
महत्त्वाचे

Driver हा database स्वतः नाही. तो application आणि database यांच्यामधील communication layer आहे.

5. मग Python DB-API म्हणजे काय?#

वेगवेगळे database drivers असतील तर प्रत्येक driver पूर्णपणे वेगळ्या style ने वापरावा लागला असता.

ही problem कमी करण्यासाठी Python मध्ये common database programming interface define करण्यात आला आहे.

त्याला Python Database API, किंवा साधारणपणे DB-API, म्हणतात.

Python DB-API 2.0 हे PEP 249 मध्ये define केलेले standard interface specification आहे.

सोप्या भाषेत:

DB-API सांगते की Python database driver ने सामान्य database operations साठी कोणत्या प्रकारचा interface द्यावा.

उदाहरणार्थ DB-API ecosystem मध्ये आपल्याला वारंवार असे concepts दिसतात:

  • connect()
  • Connection object
  • Cursor object
  • execute()
  • result fetch करणे
  • transaction commit/rollback
  • resources close करणे

यामुळे वेगवेगळ्या databases साठी syntax पूर्णपणे identical होत नाही, पण programming pattern familiar राहतो.

Remember

DB-API ही specification आहे. ती स्वतः database server नाही आणि ती स्वतः एखादा specific database driver नाही.

6. DB-API ची गरज काय?#

समजा प्रत्येक database library मध्ये connection, query execution आणि result handling यासाठी पूर्णपणे वेगळे concepts असते.

एक database बदलला की developer ला database programming ची पूर्ण mental model पुन्हा शिकावी लागली असती.

DB-API common structure देते.

त्यामुळे developer ला एक reusable pattern मिळतो:

  1. driver वापरा
  2. database शी connection मिळवा
  3. cursor मिळवा
  4. SQL execute करा
  5. गरजेनुसार result घ्या
  6. data बदलला असल्यास transaction complete करा
  7. resources close करा

Driver-specific details बदलू शकतात, पण हा basic pattern अनेक Python database libraries मध्ये ओळखीचा वाटतो.

7. connect() ची भूमिका#

connect() चे मुख्य काम म्हणजे Python program आणि target database यांच्यामध्ये database connection सुरू करणे.

महत्त्वाची गोष्ट म्हणजे connect() साधारणपणे आपल्याला एक Connection object परत देते.

Conceptually:

connect() → Connection

उदाहरण म्हणून SQLite पाहू:

PYTHON
import sqlite3

connection = sqlite3.connect(":memory:")

इथे sqlite3.connect(":memory:") एक temporary in-memory SQLite database connection तयार करते.

परत मिळालेला object आपण connection variable मध्ये ठेवला.

Key Idea

connect() query execute करण्यासाठी cursor स्वतः नसतो. त्याचा मुख्य परिणाम म्हणजे database session/resource represent करणारा Connection object मिळणे.

8. Connection object ची भूमिका#

Connection object म्हणजे Python program कडे असलेले database connection चे handle.

सोप्या mental model मध्ये:

Connection = "या database शी माझा सक्रिय communication context"

Connection object च्या माध्यमातून सामान्यतः आपण:

  • cursor तयार करू शकतो
  • write operations transaction म्हणून confirm करू शकतो
  • problem आल्यास transaction rollback करू शकतो
  • connection बंद करू शकतो

उदाहरण:

PYTHON
cursor = connection.cursor()

इथे cursor database मधून independent तयार झालेला नाही.

तो connection च्या context मधून तयार झाला आहे.

Common Mistake

Connection object आणि Cursor object एकच गोष्ट नाहीत. Connection database शी session/context represent करते; Cursor SQL operations चालवण्यासाठी working interface देते.

9. Cursor object ची भूमिका#

Cursor object चा उपयोग SQL statements database कडे पाठवण्यासाठी आणि query results हाताळण्यासाठी केला जातो.

सोप्या भाषेत:

Connection आपल्याला database शी जोडते.

Cursor त्या connection वर database कामे करायला मदत करतो.

उदाहरण:

PYTHON
cursor.execute("SELECT 1")

इथे execute() SQL statement चालवण्याची request करते.

SELECT query असल्यास cursor result set मधून data fetch करण्यासाठीही वापरला जातो.

उदाहरण:

PYTHON
row = cursor.fetchone()

fetchone() result मधील पुढची एक row देते.

Connection आणि Cursor मधील फरक#

Object मुख्य कल्पना Typical responsibility
Connection database communication context cursor तयार करणे, commit/rollback, close
Cursor SQL operation interface SQL execute करणे, query result fetch करणे

दोन्ही objects एकमेकांशी related आहेत, पण त्यांची responsibility वेगळी आहे.

10. Basic database operation lifecycle#

आता संपूर्ण flow एकत्र पाहू.

Visual Diagram80%

Rendering diagram…

हा diagram step-by-step समजून घेऊ.

Step 1 — Driver उपलब्ध असतो#

Python code specific database driver/module चा interface वापरतो.

Driver database-specific communication handle करतो.

Step 2 — connect() connection देते#

Program connect() call करतो.

यातून Connection object मिळतो.

Step 3 — Connection मधून Cursor तयार होतो#

Program connection कडून cursor मागतो.

Cursor पुढील SQL operations साठी वापरला जातो.

Step 4 — Cursor SQL execute करतो#

cursor.execute(...) मार्फत SQL statement database कडे जाते.

Step 5 — Result असल्यास तो fetch केला जातो#

SELECT सारख्या query मध्ये rows परत आल्यास cursor च्या fetch methods द्वारे त्या Python मध्ये मिळतात.

Step 6 — Data बदलला असल्यास transaction complete केली जाते#

INSERT, UPDATE किंवा DELETE सारख्या operations मध्ये काही drivers/configurations मध्ये commit() आवश्यक असते.

इथे एवढे लक्षात ठेवा: write operation successful दिसली तरी ती permanently save झाली आहे का हे transaction behavior वर अवलंबून असू शकते.

Step 7 — Resources close केले जातात#

काम संपल्यावर cursor आणि connection बंद केले जातात.

यामुळे database resources व्यवस्थित release होतात.

11. एक छोटा complete example#

आता एक छोटा SQLite example पाहू.

आपला उद्देश इथे SQLite चे advanced features शिकणे नाही. आपल्याला फक्त DB-API flow प्रत्यक्ष दिसावा हा आहे.

PYTHON
import sqlite3

connection = sqlite3.connect(":memory:")
cursor = connection.cursor()

cursor.execute("CREATE TABLE students (id INTEGER, name TEXT)")
cursor.execute("INSERT INTO students VALUES (1, 'Asha')")
connection.commit()

cursor.execute("SELECT id, name FROM students")
row = cursor.fetchone()

print(row)

cursor.close()
connection.close()

Expected Output#

TEXT
(1, 'Asha')

या code मध्ये काय झाले?

  1. sqlite3.connect(":memory:") ने Connection object दिला.
  2. connection.cursor() ने Cursor object दिला.
  3. Cursor ने table तयार करणारी SQL statement execute केली.
  4. Cursor ने एक row insert केली.
  5. connection.commit() ने write transaction confirm केली.
  6. Cursor ने SELECT query execute केली.
  7. fetchone() ने result मधून एक row Python मध्ये आणली.
  8. print(row) मुळे (1, 'Asha') दिसले.
  9. शेवटी cursor आणि connection बंद केले.

इथे सर्वात महत्त्वाची गोष्ट SQL syntax नाही.

महत्त्वाचा pattern आहे:

connect() → Connection → Cursor → execute() → fetch/commit → close

हा pattern समजला की Python database programming ची foundation तयार होते.

12. DB-API म्हणजे "same code everywhere" का?#

नाही.

हा एक common beginner misunderstanding आहे.

DB-API common programming concepts आणि interface conventions देते. पण वेगवेगळ्या drivers मध्ये काही फरक राहू शकतात.

उदाहरणार्थ:

  • connect() ला लागणारे parameters वेगळे असू शकतात.
  • SQL dialect database नुसार बदलू शकतो.
  • placeholder syntax किंवा driver-specific features बदलू शकतात.
  • काही advanced capabilities प्रत्येक driver मध्ये सारख्या नसतात.

म्हणून DB-API चा अर्थ "प्रत्येक database साठी अक्षरशः same code" असा नाही.

त्याचा अर्थ "Python database programming साठी familiar common interface pattern" असा आहे.

13. Common beginner confusions#

Confusion 1 — Driver आणि DB-API एकच आहेत का?#

नाही.

Driver म्हणजे actual implementation/library.

DB-API म्हणजे त्या implementation ने follow करावयाच्या common interface conventions चे specification.

Confusion 2 — connect() database तयार करते का?#

नेहमीच नाही.

connect() चा primary purpose म्हणजे target database शी connection/context मिळवणे.

SQLite सारख्या काही systems मध्ये file create होणे शक्य असते, पण connect() ला सर्व databases साठी "database creator" समजणे चुकीचे आहे.

Confusion 3 — Cursor म्हणजे database connection आहे का?#

नाही.

Cursor connection च्या context मध्ये SQL operations execute/fetch करण्यासाठी वापरला जातो.

Confusion 4 — SQL query थेट Python execute करते का?#

Python language स्वतः SQL database engine नाही.

Driver query database engine पर्यंत पोहोचवतो. Actual SQL processing database system करते.

महत्त्वाचे

Python code operation request करते, driver communication handle करतो, आणि database engine SQL operation process करते.

14. Interview Preparation#

What is Python DB-API?Show Answer

Python DB-API is a standard interface specification for database access modules in Python. It defines common concepts such as connections, cursors, executing statements, fetching results, transaction control, and closing resources.

Key Points:

  • DB-API is a specification, not a database.
  • DB-API 2.0 is defined by PEP 249.
  • Database drivers implement the interface.

Why interviewer asks this:

The interviewer wants to verify that you understand the abstraction behind Python database programming rather than memorizing one library.


What is the difference between a Connection object and a Cursor object?Show Answer

A Connection object represents the database connection or session context. A Cursor object is created from that connection and is used to execute SQL statements and fetch query results.

Key Points:

  • Connection manages the database communication context.
  • Cursor performs SQL operations.
  • Transaction control is typically associated with the connection.

Why interviewer asks this:

The interviewer checks whether you understand the responsibilities of the two core DB-API objects.


What does connect() usually return in a Python DB-API driver?Show Answer

connect() usually returns a Connection object representing an active connection or database session context.

Key Points:

  • The returned object is used to create cursors.
  • Connection details vary by database driver.
  • connect() itself is not a Cursor.

Why interviewer asks this:

The interviewer wants to confirm that you understand the starting point of the DB-API lifecycle.


Describe the basic database operation lifecycle in Python.Show Answer

A typical lifecycle is: obtain a database connection, create a cursor, execute SQL, fetch results when required, commit data-changing operations when required, and finally close the cursor and connection.

Key Points:

  • Driver provides the database interface.
  • Cursor executes SQL.
  • Connection coordinates the session and transaction context.
  • Resources should be closed after use.

Why interviewer asks this:

The interviewer checks whether you can explain the full flow instead of isolated method names.

15. Quick Recap#

  • Python application database शी बोलण्यासाठी database driver वापरते.
  • Driver application आणि database यांच्यामधील communication layer आहे.
  • Python DB-API common database programming interface define करते.
  • connect() साधारणपणे Connection object देते.
  • Connection database session/context represent करते.
  • Cursor SQL execute आणि results fetch करण्यासाठी वापरला जातो.
  • Basic flow: connection → cursor → execute → fetch/commit → close.
  • DB-API common pattern देते; प्रत्येक database साठी code अक्षरशः identical असेलच असे नाही.
Interactive learning

Practice lab

Apply this chapter with guided activities, immediate feedback, explanations, and retry.

Practice loads when this section enters view.