مراجعة الأسبوع الماضي: الشروط

كم كرت يطلع؟

صف الحصة
الأسبوع الثامن: تابع الاستعلام عن البيانات

سألنا القاعدة...
وين الجواب؟

نفّذنا الاستعلام وشغّلنا... والشاشة ما طبعت شي 🤔النتيجة جاهزة، بس لازم نستلمها ونطبعها (ص72-73)
ص71-72

خطوات إنشاء
الاستعلام

ص72-73

المثال 1:
كل البيانات

الكود كامل · مثال الكتاب: جدول books بقاعدة books_11grade.db
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('fut_club.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute('SELECT * FROM players')
7rows = cursor.fetchall()
8# print rows in separate lines
9for row in rows:
10 print(row)
11# close database
12connection.close()
ص73

نستلم النتيجة:
fetchall

السطر 7
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('fut_club.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute('SELECT * FROM players')
7rows = cursor.fetchall()
8# print rows in separate lines
9for row in rows:
10 print(row)
11# close database
12connection.close()
ص73

نطبع كل صف:
الحلقة for

السطرين 9 و 10
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('fut_club.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute('SELECT * FROM players')
7rows = cursor.fetchall()
8# print rows in separate lines
9for row in rows:
10 print(row)
11# close database
12connection.close()
▶ Run: main
(1, 'Salem', 'ST', 'Blue FC', 88, None)
(2, 'Fahad', 'CM', 'Red FC', 85, 27.6)
(3, 'Yousef', 'GK', 'Gold FC', 83, 7.99)
(4, 'Nasser', 'CB', 'Green FC', 80, None)
ص73

الناتج في
PyCharm

▶ Run: main
(1, 'Salem', 'ST', 'Blue FC', 88, None)
(2, 'Fahad', 'CM', 'Red FC', 85, 27.6)
(3, 'Yousef', 'GK', 'Gold FC', 83, 7.99)
(4, 'Nasser', 'CB', 'Green FC', 80, None)
Process finished with exit code 0
كل Tuple طلع في سطر مستقل: 4 لاعبين يعني 4 أسطرهذا هو الجواب اللي ما شفناه بداية الحصة ✓ (ص73)
ص74

دوال
fetch الثلاث

fetchall() تسترجع جميع الصفوف دفعة واحدة
fetchmany(size) تسترجع عدداً محدداً من الصفوف حسب المعامل size
fetchone() تسترجع صفاً واحداً فقط من النتائج في كل مرة
ص75-76

المثال 2:
عمودين بس

الكود · مثال الكتاب: title, price
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('fut_club.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute('SELECT name, rating FROM players')
7rows = cursor.fetchall()
8for row in rows:
9 print(row)
10# close database
11connection.close()
▶ Run: main
('Salem', 88)
('Fahad', 85)
('Yousef', 83)
('Nasser', 80)
Process finished with exit code 0
ص77-78

المثال 3:
مع شرط

الكود · مثال الكتاب: year > 2013
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('fut_club.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute('SELECT * FROM players WHERE rating > ?',
7 (82,))
8rows = cursor.fetchall()
9for row in rows:
10 print(row)
11# Close database
12connection.close()
▶ Run: main
(1, 'Salem', 'ST', 'Blue FC', 88, None)
(2, 'Fahad', 'CM', 'Red FC', 85, 27.6)
(3, 'Yousef', 'GK', 'Gold FC', 83, 7.99)
Process finished with exit code 0
ص79-80

ورقة العمل 6:
كل المعالم

select_data1.py · المشروع select_landmark
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('landmarks_Kuwait.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute()
7rows = cursor.fetchall()
8for row in rows:
9
10# Close the database
11connection.close()
ص81-82

ورقة العمل 7:
المعالم الثقافية

select_data2.py · المشروع select_landmark
1import sqlite3
2# Connect to the database
3connection = sqlite3.connect('landmarks_Kuwait.db')
4cursor = connection.cursor()
5# Data Retrieval
6cursor.execute()
7# Fetch the Data
8
9# Output the Data
10
11
12# Close the database
13connection.close()
الملخص

الكود كامل،
قطعة قطعة

main.py — PyCharm
1import sqlite3
2connection = sqlite3.connect('fut_club.db')
3cursor = connection.cursor()
4cursor.execute('SELECT * FROM players')
5rows = cursor.fetchall()
6for row in rows:
7 print(row)
8connection.close()
▶ Run: main
(1, 'Salem', 'ST', 'Blue FC', 88, None)
(2, 'Fahad', 'CM', 'Red FC', 85, 27.6)
(3, 'Yousef', 'GK', 'Gold FC', 83, 7.99)
(4, 'Nasser', 'CB', 'Green FC', 80, None)
Process finished with exit code 0
🏆 لوحة النقاط
↩ رجوع للشريحة 1
⬇ تصدير Excel
💾 نسخة احتياطية
📂 استرجاع نسخة
كل ضغطة = نص نقطة · خذ 💾 بنهاية كل حصة
1 / 14
‹
›
↻
🏆 النقاط