Python-ב SQLite לש םינותנ ידסמ םע הדובעל ךירדמ
.הנבומה sqlite3 לודומ תועצמאב Python-ב SQLite לש םינותנ ידסמ םע לחתה
SQLite םייסחי םינותנ ידסמ לוהינ תכרעמ איה (RDBMS) םימושיי ךותב העמטהבש תולקהו
.הנבומה sqlite3 לודומ תועצמאב Python-מ SQLite לש םינותנ ידסמ םע דובעל דציכ דמל
.ליחתנ ואוב .תויסיסב CRUD תולועפ עצבלו Python-מ SQLite לש םינותנ דסמל רבחתהל ד
הביבסה תרדגה
:ךכ הנבומה venv לודומ תועצמאב תאז תושעל לוכי התא .התוא לעפהו (טקיורפה תיירפסב)
$ python3 -m venv v1
$ source v1/bin/activate
:pip תועצמאב ותוא ןקתה זא .תויטתניס תומושר רוציל ידכ Faker-ב שמתשנ ,הז ךירדמב
$ pip3 install Faker
!תכלל ןכומ התא ,Python לש תינכדע הסרגב שמתשמ התאו Faker תא תנקתה םא זא .ותוא ן
SQLite לש םינותנ דסמל תורבחתה
.םינותנה דסמ םע רשק רוציל ונילע ,םינותנה דסמ םע היצקארטניאל ןושאר דעצכ .לחתהו
:ךכ sqlite3 לודומהמ connect()
היצקנופב שמתשהל לוכי התא ,example.d
conn = sqlite3.connect(‘example.db’)
.הדובעה תיירפסב םינותנה דסמ תא רצוי הז תרחא .וילא רבחתמ אוה זא ,םייק רבכ םינות
.םיצבקב לפטמל המוד דואמ הרוצב דבוע הז .התליאשה תואצות תא רזחאלו תותליאש עוציבל
:ךכ םע הרהצהב רשקה להנמכ רוביחב שמתשהל ליעומ הז תובורק םיתעל
import sqlite3
# Connect to the db
with sqlite3.connect('example.db') as conn:
# create db cursor
# run queries
# commit changes
.הז ךירדמב ןמסה יטקייבוא תא שרופמב רוגסנ ,תאז םע .םע קולבמ אצוי עוציבה רשאכ תי
םינותנ דסמ תואלבט תריצי
:ןמסה טקייבוא לע תארקנש execute()
תטישל התליאשה תזורחמ תא םיריבעמ
import sqlite3
# Connect to the db
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
# Create customers table
cursor.execute('''
CREATE TABLE IF NOT EXISTS customers (
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
phone TEXT,
num_orders INTEGER
);
''')
conn.commit()
print("Customers table created successfully.")
cursor.close()
:אבה טלפה תא תוארל רומא התא ,טפירקסה תא ליעפמ התא רשאכ
Output >>>
Customers table created successfully.
CRUD תולועפ עוציב
.הלועפ לכל םידרפנ םיטפירקס רוציל לכות ,הצרת םא .םינותנה דסמ תלבטב תויסיסב
תומושר תסנכה
.הצרתש תומושר המכ ףיסוהל לוכי התא לבא .תומושר 10 קר יתפסוה ,םיאירק ויהי םיטלפה
import sqlite3
import random
from faker import Faker
# Initialize Faker object
fake = Faker()
Faker.seed(24)
# Connect to the db
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
# Insert customer records
num_records = 10
for _ in range(num_records):
first_name = fake.first_name()
last_name = fake.last_name()
email = fake.email()
phone = fake.phone_number()
num_orders = random.randint(0,100)
cursor.execute('''
INSERT INTO customers (first_name, last_name, email, phone, num_orders)
VALUES (?, ?, ?, ?, ?)
''', (first_name, last_name, email, phone, num_orders))
print(f"{num_records} customer records inserted successfully.")
conn.commit()
cursor.close()
.םיכרע לש ףסוא םיריבעמו םוקימ ינייצמ ?-ב םישמתשמ ונא ,INSERT טפשמל םיכרעה
:תתל הרומא טפירקסה תלעפה
Output >>>
10 customer records inserted successfully.
תומושר ןוכדעו האירק
.התליאשה תואצות תא רזחאל ידכ ןמסה לע fetchall()
תטישבו תותליאש לי
:דוקה עטק ןלהל .1 ההזמל םאותה num_orders
-ה תא ןכדעל ידכ UPDATE תת
import sqlite3
# Connect to the db
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
# Fetch and display all customers
cursor.execute('SELECT id, first_name, last_name, email, num_orders FROM customers')
all_customers = cursor.fetchall()
print("All Customers:")
for customer in all_customers:
print(customer)
# Update num_orders for a specific customer
if all_customers:
customer_id = all_customers[0][0] # Take the ID of the first customer
new_num_orders = all_customers[0][4] + 1 # Increment num_orders by 1
cursor.execute('''
UPDATE customers
SET num_orders = ?
WHERE id = ?
''', (new_num_orders, customer_id))
print(f"Orders updated for customer ID {customer_id}: now has {new_num_orders} orders.")
conn.commit()
cursor.close()
:ןוכדעה תתליאש רחאל העדוהה תא םגו תומושרה תא םג איצומ הז
Output >>>
All Customers:
(1, 'Jennifer', 'Franco', 'jefferyjackson@example.org', 54)
(2, 'Grace', 'King', 'erinhorne@example.org', 43)
(3, 'Lori', 'Braun', 'joseph43@example.org', 99)
(4, 'Wendy', 'Hubbard', 'christophertaylor@example.com', 11)
(5, 'Morgan', 'Wright', 'arthur75@example.com', 4)
(6, 'Juan', 'Watson', 'matthewmeadows@example.net', 51)
(7, 'Randy', 'Smith', 'kmcguire@example.org', 32)
(8, 'Jimmy', 'Johnson', 'vwilliams@example.com', 64)
(9, 'Gina', 'Ellison', 'awong@example.net', 85)
(10, 'Cory', 'Joyce', 'samanthamurray@example.org', 41)
Orders updated for customer ID 1: now has 55 orders.
תומושר תקיחמ
:גצומש יפכ DELETE תרהצה ץירנ הבה ,יפיצפס חוקל ההזמ םע חוקל קוחמל ידכ
import sqlite3
# Specify the customer ID of the customer to delete
cid_to_delete = 3
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
# Execute DELETE statement to remove the customer with the specified ID
cursor.execute('''
DELETE FROM customers
WHERE id = ?
''', (cid_to_delete,))
conn.commit()
f"Customer with ID {cid_to_delete} deleted successfully.")
cursor.close()
:איצומ הז
Customer with ID 3 deleted successfully.
WHERE ףיעס תועצמאב תומושר ןוניס
:תאז גישהל לכות ךכ .(תונמזהה רפסמ הז הרקמב) ןוניסל יאנתה תא ןייצמה WHERE ףיעס
import sqlite3
# Define the threshold for the number of orders
order_threshold = 10
with sqlite3.connect('example.db') as conn:
cursor = conn.cursor()
# Fetch customers with less than 10 orders
cursor.execute('''
SELECT id, first_name, last_name, email, num_orders
FROM customers
WHERE num_orders < ?
''', (order_threshold,))
# Fetch all matching customers
filtered_customers = cursor.fetchall()
# Display filtered customers
if filtered_customers:
print("Customers with less than 10 orders:")
for customer in filtered_customers:
print(customer)
else:
print("No customers found with less than 10 orders.")
:טלפה הנהו
Output >>>
Customers with less than 10 orders:
(5, 'Morgan', 'Wright', 'arthur75@example.com', 4)
םכסמ
!חמש דודיק ,זא דע .דועו SQLite-ב תואקסע לוהינ ,הנשמ תותליאשו תופרטצה תלעפה לע
.[Python תרודהמ] םיסקדניא תועצמאב SQL תותליאש ץיאהל דציכ ארק ,םינותנ ידסמ לש ם