Hướng dẫn how do i run a sql query in python? - làm cách nào để chạy truy vấn sql trong python?
Trong bài viết này, việc tích hợp SQLite3 với Python được thảo luận. Ở đây chúng tôi sẽ thảo luận về tất cả các hoạt động CRUD trên cơ sở dữ liệu SQLite3 bằng Python. CRUD chứa bốn hoạt động chính - & NBSP; Show
Lưu ý: Điều này cần một sự hiểu biết cơ bản về SQL. & NBSP;This needs a basic understanding of SQL. Ở đây, chúng tôi sẽ kết nối SQLite với Python. Python có một thư viện gốc cho sqlite3 được gọi là sqlite3. Hãy để chúng tôi giải thích cách nó hoạt động. & NBSP;sqlite3. Let us explain how it works. 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')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')4 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')7 sqliteConnection = sqlite3.connect('gfg.db')8 sqliteConnection = sqlite3.connect('gfg.db')9 sqliteConnection = sqlite3.connect('gfg.db')3 cursor = sqliteConnection.cursor()1 Output: Connected to the database Đối tượng con trỏTrước khi di chuyển xa hơn đến SQLite3 và Python, hãy để thảo luận về đối tượng con trỏ ngắn. & NBSP;
Thực hiện 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. Python
sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')4 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')6 Connected to the database2 sqliteConnection = sqlite3.connect('gfg.db')0 Connected to the database4 cursor = sqliteConnection.cursor()1 Output: cursor = sqliteConnection.cursor()1Đối tượng con trỏ Trước khi di chuyển xa hơn đến SQLite3 và Python, hãy để thảo luận về đối tượng con trỏ ngắn. & NBSP;Python3
sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')4 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')6 Connected to the database2 sqliteConnection = sqlite3.connect('gfg.db')0 Connected to the database4 Connected to the database2 sqliteConnection = sqlite3.connect('gfg.db')0 Connected to the database4 DELETE FROM table_name [WHERE Clause]2 cursor = sqliteConnection.cursor()1 Output: cursor = sqliteConnection.cursor()1Python3
sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')4 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')6 cursor = sqliteConnection.cursor()1 Đối tượng con trỏ Trước khi di chuyển xa hơn đến SQLite3 và Python, hãy để thảo luận về đối tượng con trỏ ngắn. & NBSP; Đối tượng con trỏ được sử dụng để tạo kết nối để thực hiện các truy vấn SQL. Nó hoạt động như phần mềm trung gian giữa kết nối cơ sở dữ liệu SQLite và truy vấn SQL. Nó được tạo sau khi cung cấp kết nối với cơ sở dữ liệu SQLite. & NBSP; Con trỏ là một cấu trúc điều khiển được sử dụng để đi qua và tìm nạp các bản ghi của cơ sở dữ liệu. & NBSP; sqliteConnection = sqlite3.connect('gfg.db')36 sqliteConnection = sqlite3.connect('gfg.db')37 sqliteConnection = sqlite3.connect('gfg.db')38 sqliteConnection = sqlite3.connect('gfg.db')3 DELETE FROM table_name [WHERE Clause]2 cursor = sqliteConnection.cursor()1 Output: Tất cả các lệnh sẽ được thực thi chỉ bằng cách sử dụng đối tượng con trỏ.Thực hiện truy vấn SQLite3 - Tạo bảngfrom records is simple as inserting them. The execute method uses the SQL command of getting all the data from the table using “Select * from table_name” and all the table data can be fetched in an object in the form of a list of lists. Sau 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.Python
sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')2 sqliteConnection = sqlite3.connect('gfg.db')4 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')6 sqliteConnection = sqlite3.connect('gfg.db')37 sqliteConnection = sqlite3.connect('gfg.db')53 sqliteConnection = sqlite3.connect('gfg.db')3 cursor = sqliteConnection.cursor()1 Đối tượng con trỏ sqliteConnection = sqlite3.connect('gfg.db')36 sqliteConnection = sqlite3.connect('gfg.db')7 sqliteConnection = sqlite3.connect('gfg.db')64 Output: Trước khi di chuyển xa hơn đến SQLite3 và Python, hãy để thảo luận về đối tượng con trỏ ngắn. & NBSP;It should be noted that the database file that will be created will be in the same folder as that of the python file. If we wish to change the path of the file, change the path while opening the file. Cập nhật dữ liệuĐể cập nhật dữ liệu trong bảng SQLite3, chúng tôi 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 Update 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 các giá trị mới thành 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ác cột. & NBSP; Ví dụ: Cập nhật bảng SQLite3 bằng PythonPython3
sqliteConnection = sqlite3.connect('gfg.db')67 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')70 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')72 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')74 sqliteConnection = sqlite3.connect('gfg.db')75 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')77 sqliteConnection = sqlite3.connect('gfg.db')78 Output: Xóa dữ liệuĐể xóa dữ liệu khỏi bảng SQLite3, chúng tôi có thể sử dụng lệnh xóa. & Nbsp; DELETE FROM table_name [WHERE Clause] Ví dụ: Xóa từ bảng SQLite3 bằng PythonPython3
sqliteConnection = sqlite3.connect('gfg.db')67 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')70 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')72 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')74 sqliteConnection = sqlite3.connect('gfg.db')75 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')77 sqliteConnection = sqlite3.connect('gfg.db')78 Output: Xóa dữ liệuĐể xóa dữ liệu khỏi bảng SQLite3, chúng tôi có thể sử dụng lệnh xóa. & Nbsp; Syntax: DROP TABLE TABLE_NAME; Ví dụ: Xóa từ bảng SQLite3 bằng PythonXóa bảng Thả được sử dụng để xóa toàn bộ cơ sở dữ liệu hoặc bảng. Nó đã xóa cả hai bản ghi trong bảng cùng với cấu trúc bảng. Python3
sqliteConnection = sqlite3.connect('gfg.db')67 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')1 sqliteConnection = sqlite3.connect('gfg.db')70 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')72 sqliteConnection = sqlite3.connect('gfg.db')0 sqliteConnection = sqlite3.connect('gfg.db')74 sqliteConnection = sqlite3.connect('gfg.db')75 sqliteConnection = sqlite3.connect('gfg.db')3 sqliteConnection = sqlite3.connect('gfg.db')77 sqliteConnection = sqlite3.connect('gfg.db')78 Output: Xóa dữ liệuTo learn more about SQLit3 with Python refer to our Python SQLite3 Tutorial. Để xóa dữ liệu khỏi bảng SQLite3, chúng tôi có thể sử dụng lệnh xóa. & Nbsp;Rishabh Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Ví dụ: Xóa từ bảng SQLite3 bằng Python |