Giới thiệu về cơ sở dữ liệu trong Python Show
4 giờ 91. 2k In this course, you'll learn the basics of relational databases and how to interact with them.Xem chi tiết Mũi tên phải bắt đầu khóa họcGiới thiệu về SQLNgười bắt đầu2 giờ 149. 4K Learn how to create and query relational databases using SQL in just two hours.Xem chi tiết Mũi tên phải bắt đầu khóa họcĐể có thể thử nghiệm với các ví dụ mã trong hướng dẫn này, bạn nên cài đặt MySQL trên máy tính của mình Bạn có thể tải xuống cơ sở dữ liệu MySQL tại https. //www. mysql. com/tải xuống/ Cài đặt trình điều khiển MySQLPython cần một trình điều khiển MySQL để truy cập cơ sở dữ liệu MySQL Trong hướng dẫn này, chúng tôi sẽ sử dụng trình điều khiển "MySQL Connector" Chúng tôi khuyên bạn nên sử dụng PIP để cài đặt "MySQL Connector" PIP rất có thể đã được cài đặt trong môi trường Python của bạn Điều hướng dòng lệnh của bạn đến vị trí của PIP và nhập nội dung sau Tải xuống và cài đặt "Trình kết nối MySQL" C. \Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip cài đặt mysql-connector-python Bây giờ bạn đã tải xuống và cài đặt trình điều khiển MySQL Kiểm tra trình kết nối MySQLĐể kiểm tra xem quá trình cài đặt có thành công hay bạn đã cài đặt "Trình kết nối MySQL" chưa, hãy tạo một trang Python với nội dung sau Trong bài viết này, thảo luận về việc tích hợp SQLite3 với Python. Ở đây chúng ta sẽ thảo luận về tất cả các thao tác CRUD trên cơ sở dữ liệu SQLite3 bằng Python. CRUD chứa bốn hoạt động chính - Ghi chú. Điều này cần một sự hiểu biết cơ bản về SQL. Ở đây, chúng ta sẽ kết nối SQLite với Python. Python có một thư viện riêng cho SQLite3 được gọi là sqlite3. Hãy để chúng tôi giải thích cách nó hoạt động. Kết nối với cơ sở dữ liệu SQLite
import sqlite3
sqliteConnection = sqlite3.connect('gfg.db')
cursor = sqliteConnection.cursor() Thí dụ. Kết nối với cơ sở dữ liệu SQLite3 bằng PythonPython3
sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')4
sqliteConnection = sqlite3.connect('gfg.db')5 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')8
sqliteConnection = sqlite3.connect('gfg.db')9 cursor = sqliteConnection.cursor()0 cursor = sqliteConnection.cursor()1 cursor = sqliteConnection.cursor()2 cursor = sqliteConnection.cursor()3 sqliteConnection = sqlite3.connect('gfg.db')4
cursor = sqliteConnection.cursor()5 cursor = sqliteConnection.cursor()6 đầu ra Connected to the database đối tượng con trỏTrước khi chuyển sang SQLite3 và Python, hãy thảo luận ngắn gọn về đối tượng con trỏ.
Thực thi truy vấn SQLite3 – Tạo bảngSau khi kết nối với cơ sở dữ liệu và tạo đối tượng con trỏ, hãy xem cách thực hiện các truy vấn
Thí dụ. Tạo bảng SQLite3 bằng PythonTrong ví dụ này, chúng tôi sẽ tạo các bảng SQLite3 bằng Python. Lệnh SQL tiêu chuẩn sẽ được sử dụng để tạo các bảng con trăn
sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')4
sqliteConnection = sqlite3.connect('gfg.db')5 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')8
Connected to the database9 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;0_______1_______1 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;2 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;3 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;4 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;5 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;6 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;7
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;8 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;9
cursor = sqliteConnection.cursor()5 cursor = sqliteConnection.cursor()6 đầu ra Chèn vào bảngĐể chèn dữ liệu vào bảng, chúng ta sẽ lại viết lệnh SQL dưới dạng chuỗi và sẽ sử dụng phương thức exec() ví dụ 1. Chèn dữ liệu vào bảng SQLite3 bằng PythonPython3DELETE FROM table_name [WHERE Clause]2 DELETE FROM table_name [WHERE Clause]3
DELETE FROM table_name [WHERE Clause]4
sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')4
sqliteConnection = sqlite3.connect('gfg.db')5 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')8
DROP TABLE TABLE_NAME;7 UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;0_______1_______1 import 0
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;9
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;0____1_______1 import 6
UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition;9
cursor = sqliteConnection.cursor()5 cursor = sqliteConnection.cursor()6 đầu ra ví dụ 2. Chèn dữ liệu đầu vào của người dùngPython3DELETE FROM table_name [WHERE Clause]4
sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')4
sqliteConnection = sqlite3.connect('gfg.db')5 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')8
sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')00 sqliteConnection = sqlite3.connect('gfg.db')01 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')03 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')05 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')07 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')09 sqliteConnection = sqlite3.connect('gfg.db')10
sqliteConnection = sqlite3.connect('gfg.db')11 sqliteConnection = sqlite3.connect('gfg.db')12_______1_______1 sqliteConnection = sqlite3.connect('gfg.db')00 sqliteConnection = sqlite3.connect('gfg.db')15_______1_______02 sqliteConnection = sqlite3.connect('gfg.db')17 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')19 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')21 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')23 sqliteConnection = sqlite3.connect('gfg.db')10
sqliteConnection = sqlite3.connect('gfg.db')25 sqliteConnection = sqlite3.connect('gfg.db')26_______1_______1 sqliteConnection = sqlite3.connect('gfg.db')00 sqliteConnection = sqlite3.connect('gfg.db')29 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')31 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')33 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')35 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')29 sqliteConnection = sqlite3.connect('gfg.db')10
sqliteConnection = sqlite3.connect('gfg.db')39 sqliteConnection = sqlite3.connect('gfg.db')40_______1_______1 sqliteConnection = sqlite3.connect('gfg.db')00 sqliteConnection = sqlite3.connect('gfg.db')43 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')45 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')43 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')43 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')45 sqliteConnection = sqlite3.connect('gfg.db')10
sqliteConnection = sqlite3.connect('gfg.db')53 sqliteConnection = sqlite3.connect('gfg.db')54 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')00 sqliteConnection = sqlite3.connect('gfg.db')57_______1_______02 sqliteConnection = sqlite3.connect('gfg.db')59 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')61 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')63 sqliteConnection = sqlite3.connect('gfg.db')02 sqliteConnection = sqlite3.connect('gfg.db')61 sqliteConnection = sqlite3.connect('gfg.db')10
sqliteConnection = sqlite3.connect('gfg.db')67 sqliteConnection = sqlite3.connect('gfg.db')68 sqliteConnection = sqlite3.connect('gfg.db')69 sqliteConnection = sqlite3.connect('gfg.db')70_______2_______2 sqliteConnection = sqlite3.connect('gfg.db')07 sqliteConnection = sqlite3.connect('gfg.db')73
sqliteConnection = sqlite3.connect('gfg.db')74_______1_______75 sqliteConnection = sqlite3.connect('gfg.db')74_______1_______77 sqliteConnection = sqlite3.connect('gfg.db')78 sqliteConnection = sqlite3.connect('gfg.db')4
cursor = sqliteConnection.cursor()5 cursor = sqliteConnection.cursor()6 đầu ra Tìm nạp dữ liệuTrong phần này, chúng ta đã thảo luận cách tạo bảng và cách thêm hàng mới vào cơ sở dữ liệu. Tìm nạp dữ liệu từ các bản ghi đơn giản bằng cách chèn chúng. Phương thức thực thi sử dụng lệnh SQL để lấy tất cả dữ liệu từ bảng bằng cách sử dụng “Chọn * từ tên_bảng” và tất cả dữ liệu của bảng có thể được tìm nạp trong một đối tượng ở dạng danh sách các danh sách Thí dụ. Đọc dữ liệu từ bảng sqlite3 bằng Pythoncon trănsqliteConnection = sqlite3.connect('gfg.db')85
sqliteConnection = sqlite3.connect('gfg.db')88 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')4
sqliteConnection = sqlite3.connect('gfg.db')94 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')8
sqliteConnection = sqlite3.connect('gfg.db')98 sqliteConnection = sqlite3.connect('gfg.db')77 cursor = sqliteConnection.cursor()00 sqliteConnection = sqlite3.connect('gfg.db')4
cursor = sqliteConnection.cursor()02 cursor = sqliteConnection.cursor()03______1_______1 cursor = sqliteConnection.cursor()05
cursor = sqliteConnection.cursor()06 cursor = sqliteConnection.cursor()07 cursor = sqliteConnection.cursor()08 cursor = sqliteConnection.cursor()09 sqliteConnection = sqlite3.connect('gfg.db')67 sqliteConnection = sqlite3.connect('gfg.db')68 sqliteConnection = sqlite3.connect('gfg.db')69 cursor = sqliteConnection.cursor()13 sqliteConnection = sqlite3.connect('gfg.db')74_______2_______1 cursor = sqliteConnection.cursor()16 đầu ra Ghi chú. Cần lưu ý rằng tệp cơ sở dữ liệu sẽ được tạo sẽ nằm trong cùng thư mục với tệp python. Nếu chúng tôi muốn thay đổi đường dẫn của tệp, hãy thay đổi đường dẫn trong khi mở tệp Cập nhật dữ liệuĐể cập nhật dữ liệu trong bảng SQLite3, chúng ta sẽ sử dụng câu lệnh UPDATE. Chúng tôi có thể cập nhật các cột đơn cũng như nhiều cột bằng cách sử dụng câu lệnh CẬP NHẬT theo yêu cầu của chúng tôi UPDATE table_name SET column1 = value1, column2 = value2,… WHERE condition; Trong cú pháp trên, câu lệnh SET được sử dụng để đặt giá trị mới cho cột cụ thể và mệnh đề WHERE được sử dụng để chọn các hàng cần cập nhật cột. Thí dụ. Cập nhật bảng SQLite3 bằng PythonPython3cursor = sqliteConnection.cursor()17
cursor = sqliteConnection.cursor()20 cursor = sqliteConnection.cursor()21 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 cursor = sqliteConnection.cursor()24_______1_______4
cursor = sqliteConnection.cursor()26 cursor = sqliteConnection.cursor()27 cursor = sqliteConnection.cursor()28 sqliteConnection = sqlite3.connect('gfg.db')1 cursor = sqliteConnection.cursor()30
cursor = sqliteConnection.cursor()31 cursor = sqliteConnection.cursor()32_______2_______33 sqliteConnection = sqlite3.connect('gfg.db')4
cursor = sqliteConnection.cursor()35 cursor = sqliteConnection.cursor()36
cursor = sqliteConnection.cursor()37 cursor = sqliteConnection.cursor()38 đầu ra Xóa dữ liệuĐể xóa dữ liệu khỏi bảng SQLite3, chúng ta có thể sử dụng lệnh xóa. DELETE FROM table_name [WHERE Clause] Thí dụ. Xóa khỏi bảng SQLite3 bằng PythonPython3cursor = sqliteConnection.cursor()17
cursor = sqliteConnection.cursor()20 cursor = sqliteConnection.cursor()21 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 cursor = sqliteConnection.cursor()24_______1_______4
cursor = sqliteConnection.cursor()26 cursor = sqliteConnection.cursor()27 cursor = sqliteConnection.cursor()28 sqliteConnection = sqlite3.connect('gfg.db')1 cursor = sqliteConnection.cursor()30
cursor = sqliteConnection.cursor()31 cursor = sqliteConnection.cursor()32_______2_______55 sqliteConnection = sqlite3.connect('gfg.db')4
cursor = sqliteConnection.cursor()35 cursor = sqliteConnection.cursor()36
cursor = sqliteConnection.cursor()37 cursor = sqliteConnection.cursor()38 đầu ra Xóa bảngDROP dùng để xóa toàn bộ cơ sở dữ liệu hoặc một bảng. Nó đã xóa cả hai bản ghi trong bảng cùng với cấu trúc bảng cú pháp. DROP TABLE TABLE_NAME; Thí dụ. Thả bảng SQLite3 bằng PythonTổng số bảng trong gfg. db trước khi thả Bây giờ, hãy thả bảng Sinh viên và sau đó kiểm tra lại bảng tổng trong cơ sở dữ liệu của chúng tôi Python3cursor = sqliteConnection.cursor()17
cursor = sqliteConnection.cursor()20 cursor = sqliteConnection.cursor()21 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 cursor = sqliteConnection.cursor()24_______1_______4
cursor = sqliteConnection.cursor()26 cursor = sqliteConnection.cursor()27 cursor = sqliteConnection.cursor()28 sqliteConnection = sqlite3.connect('gfg.db')1 cursor = sqliteConnection.cursor()30
cursor = sqliteConnection.cursor()31 cursor = sqliteConnection.cursor()32_______2_______77 sqliteConnection = sqlite3.connect('gfg.db')4
cursor = sqliteConnection.cursor()35 cursor = sqliteConnection.cursor()36
cursor = sqliteConnection.cursor()37 cursor = sqliteConnection.cursor()38 đầu ra Ghi chú. Để tìm hiểu thêm về SQLit3 với Python, hãy tham khảo Hướng dẫn Python SQLite3 của chúng tôi. Bài viết này được đóng góp bởi Rishabh Bansal. Nếu bạn thích GeeksforGeeks và muốn đóng góp, bạn cũng có thể viết một bài báo bằng cách sử dụng write. chuyên viên máy tính. org hoặc gửi bài viết của bạn tới review-team@geeksforgeeks. tổ chức. Xem bài viết của bạn xuất hiện trên trang chính của GeeksforGeeks và trợ giúp các Geeks khác Vui lòng viết bình luận nếu bạn thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề thảo luận ở trên SQL và Python có thể được sử dụng cùng nhau không?Chúng ta cũng có thể sử dụng Python với SQL . Trong bài viết này, chúng ta sẽ tìm hiểu cách kết nối SQL với Python bằng mô-đun 'MySQL Connector Python'.
SQL có tốt cho Python không?Dữ liệu có cấu trúc của Python có thể được tìm nạp bằng SQL và sau đó, tất cả các phần thao tác có thể được thực hiện . Quá trình này không thể được thực hiện bằng cách sử dụng SQL một mình. Vì vậy, cuối cùng, hai ngôn ngữ này không phải là kẻ thù của nhau. Trên thực tế, không có một ngôn ngữ thứ hai là không có gì. |