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 -

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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()

Ví 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. Ví 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.  

Ví 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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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;

Ví dụ. Thả bảng SQLite3 bằng Python

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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

Làm cách nào để đọc cơ sở dữ liệu SQL trong Python?

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