Tôi có thể sử dụng SQL với Python không?

Giới thiệu về cơ sở dữ liệu trong Python

Người bắt đầu

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ọc

Giới thiệu về SQL

Người bắt đầu

2 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 MySQL

Python 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 -

Tôi có thể sử dụng SQL với Python không?

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

  • Để sử dụng SQLite, chúng ta phải nhập sqlite3
import sqlite3
  • Sau đó tạo kết nối bằng phương thức connect() và truyền tên cơ sở dữ liệu bạn muốn truy cập nếu có tệp có tên đó, nó sẽ mở tệp đó. Nếu không, Python sẽ tạo một tệp có tên đã cho
sqliteConnection = sqlite3.connect('gfg.db')
  • Sau đó, một đối tượng con trỏ được gọi để có khả năng gửi lệnh tới SQL.  
cursor = sqliteConnection.cursor()

Thí dụ. Kết nối với cơ sở dữ liệu SQLite3 bằng Python

Python3




import sqlite3

 

# connecting to the database

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ỏ.  

  • Đố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 kết nối với cơ sở dữ liệu SQLite.  
  • Con trỏ là một cấu trúc điều khiển được sử dụng để duyệt và tìm nạp các bản ghi của cơ sở dữ liệu.  
  • Tất cả các lệnh sẽ được thực thi chỉ bằng đối tượng con trỏ

Thực thi truy vấn SQLite3 – Tạo bảng

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

  • Để thực hiện một truy vấn trong cơ sở dữ liệu, hãy tạo một đối tượng và viết lệnh SQL trong đó với nhận xét. Thí dụ. - sql_comm = ”Câu lệnh SQL”
  • Và thực hiện lệnh rất dễ dàng. Gọi phương thức con trỏ thực thi () và chuyển tên của lệnh sql làm tham số trong đó. Lưu một số lệnh dưới dạng sql_comm và thực hiện chúng. Sau khi bạn thực hiện tất cả các hoạt động của mình, hãy lưu các thay đổi trong tệp bằng cách thực hiện các thay đổi đó rồi mất kết nối.  

Thí dụ. Tạo bảng SQLite3 bằng Python

Trong 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




import sqlite3

 

# connecting to the database

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 database
9

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

Tôi có thể sử dụng SQL với Python không?

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 Python

Python3




DELETE FROM table_name [WHERE Clause]
2

DELETE FROM table_name [WHERE Clause]
3

 

DELETE FROM table_name [WHERE Clause]
4

import sqlite3

 

# connecting to the database

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 import0

import1

UPDATE table_name SET column1 = value1, column2 = value2,…  
WHERE condition; 
9

 

import3

UPDATE table_name SET column1 = value1, column2 = value2,…  
WHERE condition; 
0____1_______1 import6

import7

UPDATE table_name SET column1 = value1, column2 = value2,…  
WHERE condition; 
9

 

import9

sqlite30

sqlite31

 

cursor = sqliteConnection.cursor()
5

cursor = sqliteConnection.cursor()
6

đầu ra

Tôi có thể sử dụng SQL với Python không?

ví dụ 2. Chèn dữ liệu đầu vào của người dùng

Python3




DELETE FROM table_name [WHERE Clause]
4

import sqlite3

 

# connecting to the database

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

 

# connecting to the database7

# connecting to the database8

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

 

import9

sqlite30

sqlite31

 

cursor = sqliteConnection.cursor()
5

cursor = sqliteConnection.cursor()
6

đầu ra

Tôi có thể sử dụng SQL với Python không?

Tìm nạp dữ liệu

Trong 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 Python

con trăn




sqliteConnection = sqlite3.connect('gfg.db')
85

import sqlite3

 

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

Tôi có thể sử dụng SQL với Python không?

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 Python

Python3




cursor = sqliteConnection.cursor()
17

import sqlite3

 

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

Tôi có thể sử dụng SQL với Python không?

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 Python

Python3




cursor = sqliteConnection.cursor()
17

import sqlite3

 

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

Tôi có thể sử dụng SQL với Python không?

Xóa bảng

DROP 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 Python

Tổng số bảng trong gfg. db trước khi thả

Tôi có thể sử dụng SQL với Python không?

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

Python3




cursor = sqliteConnection.cursor()
17

import sqlite3

 

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

Tôi có thể sử dụng SQL với Python không?

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ì.