Patrocinados

পাইথন ডেটাবেজঃ SQLite এবং পাইথন

0
19

জনপ্রিয় প্রায় যে কোন ডেটাবেজ ম্যানেজমেন্ট সিস্টেমই আমরা পাইথনের সাথে ব্যবহার করতে পারি। সবচেয়ে সিম্পল ডেটাবেজ ম্যানেজমেন্ট সিস্টেম হচ্ছে SQLite। একটা সম্পর্কে ভালো করে জানলে বাকি সব গুলো ডেটাবেজ একই ভাবেই ব্যবহার করা যাবে। মূল বিষয় গুলো একই। একটার সাথে একটার কিছু পার্থক্য আছে, কিছু সুবিধে রয়েছে। আমাদের প্রজেক্ট অনুযায়ী আমরা ডেটাবেজ সিলেক্ট করতে পারব। শুরু করি SQLite দিয়ে। অন্য ডেটাবেজ গুলো ব্যবহার করার জন্য আমাদের আলাদা ভাবে সে গুলো ইন্সটল করতে হয়। কিন্তু পাইথনের সাথে SQLite ইন্সটল করা থাকে। আমাদের বাড়তি কোন কফিগারেশন করতে হবে না।

SQLite ব্যবহার করার জন্য প্রথমে আমাদের ইম্পোর্ট করে নিতে হবে। আমরা যে ভার্সনটা ব্যবহার করব তা হচ্ছে 3, তাই ইম্পোর্ট করার জন্য আমাদের লিখতে হবেঃ

import sqlite3

ডাটাবেজ তৈরি করা ভয়াবহ রকম সোজা। তার জন্য লিখতে হবেঃ

import sqlite3

db = sqlite3.connect('friends.db')

এখানে friends.db হচ্ছে ডেটাবেজ নেম। যে নামে আমরা ডেটাবেজ তৈরি করব।

import sqlite3

db = sqlite3.connect('friends.db')

db.execute('drop table if exists friends')

db.execute('create table friends (i int, friend text)')

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shuvo ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 2, 'Tuhin ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 3, 'Tanvir ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 4, 'Dipu ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 5, 'Siyam ' ))
db.commit()

result = db.execute('select * from friends')
for row in result :
    print (row)
   

প্রোগ্রামের শুরুতেই আমরা দেখে নিয়েছি friends নামে কোন টেবিল আছে কিনা, যদি থাকে, তাহলে আমরা তা রিমুভ করব drop কমান্ড দিয়ে। কারণ আমরা এরপর friends নামে একটা টেবিল তৈরি করব।

db.execute('drop table if exists friends')

টেবিল তৈরি করার কমান্ডঃ

db.execute('create table friends (i int, friend text)') 

যেখানে আমরা দুইটা কলাম তৈরি কব। একটা হচ্ছে i নামে। যেটায় ইন্টিজার ভ্যালু রাখব। আরেকটা হচ্ছে friend নামে, যেটায় আমরা টেক্সট রাখব।

এবার আমরা ডেটাবেজে কয়েকটি ডেটা রাখব, তার জন্যঃ

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shuvo ' ))

insert into friends(i, friend) এর friends হচ্ছে ডেটাবেজ টেবিলের নাম। i, friend হচ্ছে দুটি কলাম, যেখানে আমরা ডেটা রাখব। values (?,?) এখানে ? হচ্ছে প্লেসহোল্ডার। ( 1, ‘Shubo ‘ ) হচ্ছে ভ্যালু। প্রথম প্লেস হোল্ডারের নাম হচ্ছে 1, দ্বিতীয় প্লেসহোল্ডারের মান হচ্ছে Shuvo.

এভাবে আমরা কয়েকটি মান রেখেছি।

এর পরের কাজ হচ্ছে ডেটা বেজ থেকে ডেটা read করা বা পড়া।

তার জন্য db.execute(‘select * from friends’) দিয়ে friends টেবিলের সব ডেটা আমরা পড়ে result নামক ভ্যারিয়েবলে রেখেছি। result ভ্যারিয়েবলে ডেটা গুলো টাপল অনুযায়ী রয়েছে। আমরা এবার তা প্রিন্ট করতে পারিঃ

for row in result :
     print (row)

ডেটা গুলো অর্ডার করতে যে রো দিয়ে অর্ডার করতে চাই, তা উল্লেখ করে দিতে পারি, তার জন্য SQL কমান্ড হচ্ছেঃ select * from friends order by i

সম্পুর্ণ প্রোগ্রামঃ

import sqlite3

db = sqlite3.connect('friends.db')

db.execute('drop table if exists friends')

db.execute('create table friends (i int, friend text)')

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shubo ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 2, 'Tuhin ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 3, 'Tanvir ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 4, 'Dipu ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 5, 'Siyam ' ))
db.commit()

result = db.execute('select * from friends order by i')
for row in result :
    print (row)

আমরা চাইলে ফ্রেন্ড দিয়েও অর্ডার করতে পারিঃ

import sqlite3

db = sqlite3.connect('friends.db')

db.execute('drop table if exists friends')

db.execute('create table friends (i int, friend text)')

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shubo ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 2, 'Tuhin ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 3, 'Tanvir ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 4, 'Dipu ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 5, 'Siyam ' ))
db.commit()

result = db.execute('select * from friends order by friend')
for row in result :
    print (row)
   

ডেটা বেজে অনেক ডেটা থাকবে। উপরের প্রোগ্রামে আমরা friends টেবিল থেকে সব গুলো ডেটা রিড করেছি। আমরা চাইলে শুধু i কলাম বা শুধু friend কলাম থেকে ডেটা রিড করতে পারি। যেমন আমরা শুধু friend কলামের ডেটা গুলো রিড করব, তার SQL কমান্ড হছেঃ select friend from friends order by friend

সম্পুর্ণ প্রোগ্রামঃ

import sqlite3

db = sqlite3.connect('friends.db')

db.execute('drop table if exists friends')

db.execute('create table friends (i int, friend text)')

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shubo ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 2, 'Tuhin ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 3, 'Tanvir ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 4, 'Dipu ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 5, 'Siyam ' ))
db.commit()

result = db.execute('select friend from friends order by friend')
for row in result :
    print (row)
   

UPDATE:

আমরা যদি ডেটা বেজে কোন ডেটা ভুল লিখি বা আমাদের ডেটা আপডেট করতে হয়, তাহলে আমরা ডেটা আপডেট করতে পারি। যেমন আমরা যদি উপরের ডেটাতে Shuvo নামটি আপডেট করে Sammo রাখি, তার জন্য আমাদের SQL কমান্ড হবেঃ

update friends set friend = ? where i = ?', ( 'Sammo ', 1)

where দিয়ে কোন ইনডেক্স এর ডেটা আপডেট করব, তা ঠিক করে দিয়েছি। সম্পুর্ণ প্রোগ্রামঃ

import sqlite3

db = sqlite3.connect('friends.db')

db.execute('drop table if exists friends')

db.execute('create table friends (i int, friend text)')

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shubo ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 2, 'Tuhin ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 3, 'Tanvir ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 4, 'Dipu ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 5, 'Siyam ' ))
db.commit()

print ("nBefore Update: n" )
result = db.execute('select * from friends')
for row in result :
    print (row)
   
db.execute('update friends set friend = ? where i = ?', ( 'Sammo ', 1) )
db.commit()

print ("n After Update: n" )
result = db.execute('select * from friends')
for row in result :
    print (row)

DELETE:

ডিলেট করার সময় বলে দিতে হবে আমরা কি ডিলেট করতে চাই। যেমন একটি row ডিলেট করতে চাইলে লিখব এভাবেঃ

delete from friends where i = 1

সম্পুর্ণ প্রোগ্রামঃ

import sqlite3

db = sqlite3.connect('friends.db')

db.execute('drop table if exists friends')

db.execute('create table friends (i int, friend text)')

db.execute('insert into friends(i, friend) values (?,?)', ( 1, 'Shubo ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 2, 'Tuhin ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 3, 'Tanvir ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 4, 'Dipu ' ))
db.execute('insert into friends(i, friend) values (?,?)', ( 5, 'Siyam ' ))
db.commit()

print ("nBefore delete: n" )
result = db.execute('select * from friends')
for row in result :
    print (row)
   
db.execute('delete from friends where i = 1' )
db.commit()

print ("n After delete: n" )
result = db.execute('select * from friends')
for row in result :
    print (row)

ডেটাবেজ এর প্রধান চারটি অপারেশন হচ্ছে Create, Read, Updated, Delete. সংক্ষেপে CRUD. এ চারটি অপারেশন হচ্ছে মৈলিক। আরো অনেক কিছুই করা যায়। SQL অপারেশন বলে। SQL এর উপর বিস্তারিত পড়ে সে গুলো জানা যাবে। w3school থেকে SQL টিউটোরিয়াল বা ইউটিউব থেকে SQL + Python এর ভিডিও টিউটোরিয়াল দেখে বিস্তারিত শিখে নেওয়া যাবে।

Buscar
Categorías
Read More
Nulled
Socialified v2.0 Nulled - Social Media App Clone (Instagram, Facebook, Linked in etc) + Node.js Socket Chat + Calling + Live Source
Socialified v2.0 Nulled – Social Media App Clone (Instagram, Facebook, Linked in etc) +...
By Nettumi 2026-09-18 03:26:27 0 44
Tech
বাংলাদেশে যে আইটি, বিজ্ঞান ও প্রযুক্তিতে বিশ্ব | Techtunes | টেকটিউনস
মোবাইলের মাধ্যমে চ্যাট করার কিছু দারুণ সফটওয়্যার মো. আমিনুল ইসলাম সজীব
By Nettumi 2026-09-26 11:49:37 0 33
Nulled
Zaiproty v4.6 Nulled - Property Management Laravel Script
Zaiproty v4.6 Nulled – Property Management Laravel Script Free Download. Zaiproty...
By Nettumi 2026-09-17 18:25:59 0 50
Nulled
Rocket BeApp v2.1 - Flutter Web Converter App with Admin Panel
Rocket BeApp v2.1 – Flutter Web Converter App with Admin Panel Source Code Free Download....
By Nettumi 2026-09-18 03:49:39 0 45
Nulled
গান শোনার মজা দ্বিগুণ করতে এখন লিরিক্স স্ক্রিনেই বাজিমাত করলো Spotify-এর জাদুকরী ফিচার! | Techtunes | টেকটিউনস
ফ্লিকার ভিডিও ব্রাউজার – ফটো আপলোডিং এবং পাবলিশিং পোর্টাল দুঃসাহসী টিনটিন
By Nettumi 2026-09-21 05:31:13 0 45
‎ ‎ ‎
Nettumi https://nettumi.com