Python SQLite Database क्या है? Create, Insert, Select, Update, Delete in Hindi | RBSE Class 12

इस लेख में: Python में SQLite Database क्या है, database connection कैसे बनाते हैं, table कैसे create करते हैं तथा INSERT, SELECT, UPDATE और DELETE operations कैसे perform करते हैं, यह आसान Hindi में examples के साथ समझेंगे।

SQLite Database क्या है?

SQLite एक lightweight और serverless relational database engine है। Python में SQLite database के साथ काम करने के लिए sqlite3 module उपलब्ध है।

SQLite database में data को tables के रूप में store किया जा सकता है। इसमें SQL commands का उपयोग करके data को insert, retrieve, update और delete किया जा सकता है।

Important: Python में SQLite के लिए sqlite3 module standard Python installation के साथ उपलब्ध होता है। सामान्य SQLite database operations के लिए अलग database server की आवश्यकता नहीं होती।

Database क्या है?

Database ऐसा organized collection है जिसमें data को व्यवस्थित तरीके से store और manage किया जाता है।

उदाहरण के लिए किसी school के database में students की information जैसे नाम, roll number और marks store किए जा सकते हैं।

SQLite की विशेषताएँ

  • यह lightweight database engine है।
  • यह serverless architecture का उपयोग करता है।
  • Database एक file में store किया जा सकता है।
  • SQL commands का उपयोग किया जा सकता है।
  • Python के साथ sqlite3 module द्वारा आसानी से काम किया जा सकता है।

sqlite3 Module Import करना

import sqlite3

SQLite Database से Connect करना

Database के साथ connection बनाने के लिए sqlite3.connect() का उपयोग किया जा सकता है।

import sqlite3

connection = sqlite3.connect("school.db")

print("Database connected successfully")

यदि school.db file मौजूद नहीं है, तो SQLite सामान्यतः उसे create कर सकता है।

Connection Close करना

काम पूरा होने के बाद database connection को close करना अच्छी practice है।

connection.close()

Cursor क्या है?

Database पर SQL statements execute करने के लिए cursor object का उपयोग किया जा सकता है।

import sqlite3

connection = sqlite3.connect("school.db")

cursor = connection.cursor()

print("Cursor created")

connection.close()

Database Table Create करना

Table बनाने के लिए SQL में CREATE TABLE statement का उपयोग किया जाता है।

import sqlite3

connection = sqlite3.connect("school.db")

cursor = connection.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
    id INTEGER PRIMARY KEY,
    name TEXT,
    marks INTEGER
)
""")

connection.commit()
connection.close()

CREATE TABLE का मतलब

Column Data Type अर्थ
id INTEGER Student की पहचान
name TEXT Student का नाम
marks INTEGER Student के marks

PRIMARY KEY क्या है?

PRIMARY KEY table की प्रत्येक row को uniquely identify करने के लिए उपयोग की जाती है।

ऊपर दिए गए example में id को primary key बनाया गया है।

INSERT Operation

Database table में नया record जोड़ने के लिए SQL की INSERT INTO statement का उपयोग किया जाता है।

import sqlite3

connection = sqlite3.connect("school.db")
cursor = connection.cursor()

cursor.execute(
    "INSERT INTO students (name, marks) VALUES (?, ?)",
    ("Rahul", 85)
)

connection.commit()
connection.close()
Important: Python से SQL values भेजते समय parameterized queries का उपयोग करना अच्छी practice है। ऊपर ? placeholders का उपयोग किया गया है।

Multiple Records Insert करना

import sqlite3

connection = sqlite3.connect("school.db")
cursor = connection.cursor()

students = [
    ("Rahul", 85),
    ("Amit", 72),
    ("Sita", 91),
    ("Mohan", 68)
]

cursor.executemany(
    "INSERT INTO students (name, marks) VALUES (?, ?)",
    students
)

connection.commit()
connection.close()

SELECT Operation

Database से records प्राप्त करने के लिए SELECT statement का उपयोग किया जाता है।

import sqlite3

connection = sqlite3.connect("school.db")
cursor = connection.cursor()

cursor.execute("SELECT * FROM students")

rows = cursor.fetchall()

for row in rows:
    print(row)

connection.close()

fetchone() Method

fetchone() result में से एक row प्राप्त करने के लिए उपयोग किया जा सकता है।

cursor.execute("SELECT * FROM students")

row = cursor.fetchone()

print(row)

fetchall() Method

fetchall() सभी available rows को प्राप्त करने के लिए उपयोग किया जा सकता है।

cursor.execute("SELECT * FROM students")

rows = cursor.fetchall()

for row in rows:
    print(row)

WHERE Clause

किसी condition के आधार पर records प्राप्त करने के लिए WHERE clause का उपयोग किया जा सकता है।

cursor.execute(
    "SELECT * FROM students WHERE marks > ?",
    (80,)
)

rows = cursor.fetchall()

for row in rows:
    print(row)

UPDATE Operation

Existing record को बदलने के लिए UPDATE statement का उपयोग किया जाता है।

cursor.execute(
    "UPDATE students SET marks = ? WHERE id = ?",
    (90, 1)
)

connection.commit()

DELETE Operation

Record को हटाने के लिए DELETE FROM statement का उपयोग किया जाता है।

cursor.execute(
    "DELETE FROM students WHERE id = ?",
    (1,)
)

connection.commit()
Important: UPDATE और DELETE जैसे operations के बाद changes को permanently save करने के लिए सामान्यतः commit() किया जाता है।

commit() क्या है?

commit() database transaction में किए गए changes को save करने के लिए उपयोग किया जाता है।

connection.commit()

rollback() क्या है?

यदि transaction में समस्या आती है और किए गए changes को वापस करना हो, तो rollback() का उपयोग किया जा सकता है।

connection.rollback()

Table के सभी Records देखना

cursor.execute("SELECT * FROM students")

for row in cursor.fetchall():
    print(row)

Specific Columns Select करना

cursor.execute(
    "SELECT name, marks FROM students"
)

for row in cursor.fetchall():
    print(row)

ORDER BY का उपयोग

Records को किसी column के आधार पर sort करने के लिए ORDER BY का उपयोग किया जा सकता है।

cursor.execute(
    "SELECT * FROM students ORDER BY marks DESC"
)

for row in cursor.fetchall():
    print(row)

COUNT() Function

Table में records की संख्या प्राप्त करने के लिए SQL के COUNT() function का उपयोग किया जा सकता है।

cursor.execute(
    "SELECT COUNT(*) FROM students"
)

count = cursor.fetchone()[0]

print("Total Students:", count)

Complete SQLite Program

नीचे एक complete example दिया गया है जिसमें database connection, table creation, records insert और records display करना शामिल है।

import sqlite3

connection = sqlite3.connect("school.db")
cursor = connection.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS students (
    id INTEGER PRIMARY KEY,
    name TEXT,
    marks INTEGER
)
""")

students = [
    ("Rahul", 85),
    ("Amit", 72),
    ("Sita", 91)
]

cursor.executemany(
    "INSERT INTO students (name, marks) VALUES (?, ?)",
    students
)

connection.commit()

cursor.execute("SELECT * FROM students")

rows = cursor.fetchall()

for row in rows:
    print(row)

connection.close()

SQLite में CRUD क्या है?

Database के चार basic operations को अक्सर CRUD कहा जाता है।

CRUD SQL Operation उपयोग
Create INSERT नया record जोड़ना
Read SELECT Data पढ़ना
Update UPDATE Existing data बदलना
Delete DELETE Data हटाना

Important SQLite Methods

Method / Command उपयोग
sqlite3.connect() Database connection बनाने के लिए
cursor() Cursor object बनाने के लिए
execute() SQL statement execute करने के लिए
executemany() Multiple parameter sets के साथ statement execute करने के लिए
fetchone() एक row प्राप्त करने के लिए
fetchall() सभी rows प्राप्त करने के लिए
commit() Changes save करने के लिए
rollback() Transaction changes वापस करने के लिए
close() Connection बंद करने के लिए

Important SQL Commands

SQL Command उपयोग
CREATE TABLE Table बनाने के लिए
INSERT INTO Record जोड़ने के लिए
SELECT Data प्राप्त करने के लिए
UPDATE Record बदलने के लिए
DELETE Record हटाने के लिए
WHERE Condition लगाने के लिए
ORDER BY Data sort करने के लिए

RBSE Class 12 Important Questions

  1. SQLite क्या है?
  2. Database क्या है?
  3. Python में SQLite के लिए कौन-सा module उपयोग किया जाता है?
  4. sqlite3.connect() का उपयोग क्या है?
  5. Cursor क्या है?
  6. cursor.execute() का उपयोग क्या है?
  7. CREATE TABLE statement क्या है?
  8. PRIMARY KEY क्या है?
  9. INSERT statement का उपयोग क्या है?
  10. SELECT statement क्या करता है?
  11. fetchone() और fetchall() में अंतर लिखिए।
  12. UPDATE statement का उपयोग क्या है?
  13. DELETE statement का उपयोग क्या है?
  14. commit() method क्या करता है?
  15. rollback() method क्या करता है?
  16. close() method का उपयोग क्या है?
  17. CRUD का पूरा नाम क्या है?
  18. WHERE clause का उपयोग क्यों किया जाता है?
  19. ORDER BY का उपयोग क्या है?
  20. Python में SQLite database का एक program लिखिए।

Frequently Asked Questions (FAQs)

SQLite क्या है?

SQLite एक lightweight और serverless relational database engine है।

Python में SQLite कैसे use करते हैं?

Python में SQLite database के साथ काम करने के लिए सामान्यतः sqlite3 module का उपयोग किया जाता है।

Database connection कैसे बनाते हैं?

sqlite3.connect() function का उपयोग करके SQLite database से connection बनाया जा सकता है।

Data insert करने के लिए कौन-सी SQL command है?

Data insert करने के लिए INSERT INTO command का उपयोग किया जाता है।

Database से data पढ़ने के लिए कौन-सी command है?

Data प्राप्त करने के लिए SELECT command का उपयोग किया जाता है।

commit() क्यों किया जाता है?

Database में किए गए changes को save करने के लिए सामान्यतः commit() किया जाता है।

Python SQLite में CRUD क्या है?

CRUD का अर्थ Create, Read, Update और Delete है। Database applications में ये चार basic data operations हैं।

अब अगला Topic पढ़ें

RBSE Python Study Tip: SQLite में सबसे पहले connection, cursor और table creation को समझें। इसके बाद INSERT, SELECT, UPDATE और DELETE यानी CRUD operations की practice करें। Exam में SQL command और Python code दोनों के relationship को समझना बहुत उपयोगी रहेगा।
Official Reference:

Python में SQLite की official documentation के लिए Python documentation देखें।

Python sqlite3 Documentation