Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?
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 - Show
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() Ví 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
Ví 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 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 Ví 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. Ví 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] Ví 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; Ví 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 Làm cách nào để đọc cơ sở dữ liệu SQL trong gấu trúc?Tôi sẽ sử dụng các bước sau để giải thích cách sử dụng pandas read_sql(). . Cú pháp của read_sql() Tạo bảng cơ sở dữ liệu trên sqlite3 Chèn dữ liệu vào bảng Thực hiện truy vấn SQL bằng cách sử dụng pandas read_sql() Sử dụng read_sql_query() Sử dụng read_sql_table() Lọc các hàng từ bảng SQL Làm cách nào để đọc dữ liệu từ cơ sở dữ liệu SQL?CHỌN câu lệnh
. Mệnh đề SELECT chỉ định một hoặc nhiều cột sẽ được truy xuất; . Để truy xuất tất cả các cột, hãy sử dụng ký tự đại diện * (dấu hoa thị). SELECT column1, column2 FROM table1, table2 WHERE column2='value'; In the above SQL statement: The SELECT clause specifies one or more columns to be retrieved; to specify multiple columns, use a comma and a space between column names. To retrieve all columns, use the wild card * (an asterisk).
Python có thể lấy dữ liệu từ cơ sở dữ liệu SQL không?Để đọc dữ liệu từ máy chủ SQL sang python, bạn cần có thư viện pyodbc . Thư viện này có thể được cài đặt bằng lệnh bên dưới trên máy tính xách tay jupyter. Lệnh này cũng có thể được thực thi trong dấu nhắc lệnh mà không có dấu chấm than “. ”.
Làm cách nào để truy vấn cơ sở dữ liệu bằng Python?SQLite Python. Truy vấn dữ liệu . Đầu tiên, thiết lập kết nối tới cơ sở dữ liệu SQLite bằng cách tạo đối tượng Kết nối Tiếp theo, tạo đối tượng Con trỏ bằng phương thức con trỏ của đối tượng Kết nối Sau đó, thực hiện một câu lệnh SELECT Sau đó, gọi phương thức fetchall() của đối tượng con trỏ để lấy dữ liệu |