|
| 1 | +import sqlite3 |
| 2 | + |
| 3 | +conn = sqlite3.connect('youtube_videos.db') |
| 4 | +cursor = conn.cursor() |
| 5 | + |
| 6 | +# Create table |
| 7 | +cursor.execute(''' |
| 8 | + CREATE TABLE IF NOT EXISTS videos ( |
| 9 | + id INTEGER PRIMARY KEY, |
| 10 | + name TEXT NOT NULL, |
| 11 | + time TEXT NOT NULL |
| 12 | + ) |
| 13 | +''') |
| 14 | + |
| 15 | +# List all videos |
| 16 | +def list_videos(): |
| 17 | + cursor.execute("SELECT * FROM videos") |
| 18 | + for row in cursor.fetchall(): |
| 19 | + print(row) |
| 20 | + |
| 21 | +# Add video |
| 22 | +def add_video(name, time): |
| 23 | + cursor.execute("INSERT INTO videos (name, time) VALUES (?, ?)", (name, time)) |
| 24 | + conn.commit() |
| 25 | + |
| 26 | +# Update video |
| 27 | +def update_video(video_id, new_name, new_time): |
| 28 | + cursor.execute("UPDATE videos SET name = ?, time = ? WHERE id = ?", (new_name, new_time, video_id)) |
| 29 | + conn.commit() |
| 30 | + |
| 31 | +# Delete video |
| 32 | +def delete_video(video_id): |
| 33 | + cursor.execute("DELETE FROM videos WHERE id = ?", (video_id,)) |
| 34 | + conn.commit() |
| 35 | + |
| 36 | +# Main menu |
| 37 | +def main(): |
| 38 | + while True: |
| 39 | + print('\nYoutube manager app with DB') |
| 40 | + print('1. List all videos') |
| 41 | + print('2. Add video') |
| 42 | + print('3. Update video') |
| 43 | + print('4. Delete video') |
| 44 | + print('5. Exit app') |
| 45 | + |
| 46 | + choice = input('Enter your choice: ') |
| 47 | + |
| 48 | + if choice == '1': |
| 49 | + list_videos() |
| 50 | + elif choice == '2': |
| 51 | + name = input('Enter the video name: ') |
| 52 | + time = input('Enter the time: ') |
| 53 | + add_video(name, time) |
| 54 | + elif choice == '3': |
| 55 | + video_id = int(input('Enter the Video ID to update: ')) |
| 56 | + name = input('Enter the new video name: ') |
| 57 | + time = input('Enter the new time: ') |
| 58 | + update_video(video_id, name, time) |
| 59 | + elif choice == '4': |
| 60 | + video_id = int(input('Enter the Video ID to delete: ')) |
| 61 | + delete_video(video_id) |
| 62 | + elif choice == '5': |
| 63 | + break |
| 64 | + else: |
| 65 | + print('Invalid choice') |
| 66 | + conn.close() |
| 67 | + |
| 68 | +if __name__ == '__main__': |
| 69 | + main() |
0 commit comments