Chèn tuple vào bảng sql python

MySQL là Hệ thống quản lý cơ sở dữ liệu quan hệ (RDBMS) trong khi Ngôn ngữ truy vấn có cấu trúc (SQL) là ngôn ngữ được sử dụng để xử lý RDBMS bằng các lệnh i. e Tạo, chèn, cập nhật và xóa dữ liệu khỏi cơ sở dữ liệu. Các lệnh SQL không phân biệt chữ hoa chữ thường. e CREATE và tạo signify cùng một lệnh

Show

Ghi chú. Trước khi chèn dữ liệu vào cơ sở dữ liệu, chúng ta cần tạo một bảng. Để làm như vậy, hãy tham khảo Python. Bảng tạo MySQL

Chèn dữ liệu

Bạn có thể chèn một hàng hoặc nhiều hàng cùng một lúc. Cần có mã trình kết nối để kết nối các lệnh với cơ sở dữ liệu cụ thể

Truy vấn trình kết nối




7 details inserted
1

7 details inserted
2

7 details inserted
3

7 details inserted
4

7 details inserted
5

7 details inserted
6
7 details inserted
7

7 details inserted
5

7 details inserted
5

7 details inserted
1
7 details inserted
2
7 details inserted
3

7 details inserted
4
7 details inserted
5
7 details inserted
2
7 details inserted
7
7 details inserted
8

7 details inserted
4
7 details inserted
10
7 details inserted
2
7 details inserted
12
7 details inserted
8

7 details inserted
4
7 details inserted
15
7 details inserted
2
7 details inserted
17
7 details inserted
8

7 details inserted
4
7 details inserted
20______02
7 details inserted
22

7 details inserted
23

7 details inserted
5

7 details inserted
25
7 details inserted
2
7 details inserted
27

Bây giờ, Chèn vào Truy vấn có thể được viết như sau

Thí dụ. Giả sử bản ghi trông như thế này -

Chèn tuple vào bảng sql python




7 details inserted
28
7 details inserted
2
7 details inserted
30

7 details inserted
31
7 details inserted
2
7 details inserted
33
7 details inserted
34
7 details inserted
8
7 details inserted
36
7 details inserted
37

7 details inserted
5

7 details inserted
39

7 details inserted
40

7 details inserted
5

7 details inserted
42
7 details inserted
43
7 details inserted
44
7 details inserted
37

7 details inserted
5

7 details inserted
47

7 details inserted
48

đầu ra

7 details inserted
0

Chèn tuple vào bảng sql python

Để chèn nhiều giá trị cùng một lúc, phương thức

7 details inserted
49 được sử dụng. Phương thức này lặp qua chuỗi các tham số, truyền tham số hiện tại cho phương thức thực thi

Trong bài viết này, chúng tôi sẽ cố gắng chèn các bản ghi và kiểm tra xem nó có TỒN TẠI hay không. Điều kiện EXISTS trong SQL được sử dụng để kiểm tra xem kết quả của một truy vấn lồng nhau tương quan có rỗng (không chứa bộ dữ liệu nào) hay không. Nó có thể được sử dụng để INSERT, SELECT, UPDATE, hoặc DELETE câu lệnh.  

Truy vấn chúng tôi đang sử dụng chương trình python là

CHÈN VÀO tên bảng(col1, col2, col3) \    CHỌN * TỪ (CHỌN val1, val2, val3) làm tạm thời \    WHERE NOT EXISTS \    (CHỌN khóa chính TỪ tên bảng WHERE khóa chính = bản ghi được chèn) GIỚI HẠN

Giả sử chúng ta có một cơ sở dữ liệu tên là test và một bảng có tên trong geeksforgeeks. Dưới đây là lược đồ và dữ liệu của bảng

Chèn tuple vào bảng sql python

cơ sở dữ liệu

Vì mỗi cá nhân có một số nhận dạng duy nhất, chúng tôi sẽ chèn bản ghi và kiểm tra ID_NO của người đó. Nếu ID_NO chưa có trong bảng, bản ghi sẽ được chèn, nếu không bản ghi sẽ bị loại bỏ. Hãy cho chúng tôi hiểu bằng cách sử dụng các ví dụ dưới đây

ví dụ 1. Khi bản ghi không tồn tại trong bảng

Python3




7 details inserted
9

7 details inserted
6
7 details inserted
1

 

 

7 details inserted
2

7 details inserted
3
7 details inserted
4
7 details inserted
5

7 details inserted
6
7 details inserted
7

7 details inserted
8

7 details inserted
6______261____64
7 details inserted
63
7 details inserted
64

7 details inserted
6

7 details inserted
6
7 details inserted
67

7 details inserted
6______269
7 details inserted
4
7 details inserted
11
7 details inserted
64

7 details inserted
6

7 details inserted
6
7 details inserted
15

7 details inserted
6______617
7 details inserted
4
7 details inserted
19
7 details inserted
64

7 details inserted
6

7 details inserted
6____623

7 details inserted
6______625____64
7 details inserted
27
7 details inserted
64

7 details inserted
6

7 details inserted
6
7 details inserted
31

7 details inserted
6______633
7 details inserted
4
7 details inserted
35

7 details inserted
36

 

 

7 details inserted
37

7 details inserted
38
7 details inserted
4
7 details inserted
40

 

 

7 details inserted
41

7 details inserted
42____643
7 details inserted
36

7 details inserted
45
7 details inserted
4
7 details inserted
47

________ 648 ________ 649 ________ 650 ________ 651

7 details inserted
6____653____654

 

7 details inserted
6

7 details inserted
56

7 details inserted
42

7 details inserted
6
7 details inserted
59

7 details inserted
6_______661
7 details inserted
62
7 details inserted
63
7 details inserted
64
7 details inserted
65
7 details inserted
64
7 details inserted
67
7 details inserted
64
7 details inserted
69
7 details inserted
64
7 details inserted
71
7 details inserted
64
7 details inserted
73
7 details inserted
74

7 details inserted
6_______676
7 details inserted
77
7 details inserted
78

7 details inserted
6______680
7 details inserted
63
7 details inserted
82
7 details inserted
4
7 details inserted
73
7 details inserted
85
7 details inserted
86
7 details inserted
87

7 details inserted
53____689
7 details inserted
600
7 details inserted
36

 

 

7 details inserted
602

7 details inserted
42____643
7 details inserted
36

7 details inserted
45
7 details inserted
4
7 details inserted
47

________ 648 ________ 649 ________ 650 ________ 651

7 details inserted
6____653____654

7 details inserted
42____2617____636

 

 

7 details inserted
619

7 details inserted
620

đầu ra

Chèn tuple vào bảng sql python

Đầu ra Python sau khi chèn

Ở phần đầu ra ở trên, chúng ta có thể thấy rằng bản ghi (‘Thomas’,’UK’,30,1892345670,’IND100′) đã được chèn vào bảng MySQL

ví dụ 2. Khi bản ghi đã tồn tại

Dưới đây là lược đồ và dữ liệu của bảng geeksdemo trong cơ sở dữ liệu geek

Chèn tuple vào bảng sql python

Bây giờ, chúng tôi sẽ cố gắng chèn một bản ghi đã tồn tại.  

Python3




7 details inserted
9

7 details inserted
6
7 details inserted
1

 

 

7 details inserted
2

7 details inserted
3
7 details inserted
4
7 details inserted
5

7 details inserted
6
7 details inserted
7

7 details inserted
6______261____64
7 details inserted
63
7 details inserted
64

7 details inserted
635

7 details inserted
6
7 details inserted
67

7 details inserted
6______269
7 details inserted
4
7 details inserted
11
7 details inserted
64

7 details inserted
635

7 details inserted
6
7 details inserted
15

7 details inserted
6______617
7 details inserted
4
7 details inserted
649

7 details inserted
6

7 details inserted
6____623

7 details inserted
6______625____64
7 details inserted
27
7 details inserted
64

7 details inserted
6

7 details inserted
6
7 details inserted
31

7 details inserted
6______633
7 details inserted
4
7 details inserted
664

7 details inserted
36

 

 

7 details inserted
37

7 details inserted
38
7 details inserted
4
7 details inserted
40

 

 

7 details inserted
41

7 details inserted
42____2672____636

7 details inserted
45
7 details inserted
4
7 details inserted
47

________ 648 ________ 649 ________ 650 ________ 651

7 details inserted
6____653____654

7 details inserted
6

7 details inserted
6

7 details inserted
56

7 details inserted
42

7 details inserted
6____2689
7 details inserted
690
7 details inserted
691

7 details inserted
6_______661
7 details inserted
62
7 details inserted
63
7 details inserted
64
7 details inserted
697
7 details inserted
64
7 details inserted
65
7 details inserted
64
7 details inserted
101
7 details inserted
64
7 details inserted
103
7 details inserted
74

7 details inserted
6_______676
7 details inserted
77
7 details inserted
78

7 details inserted
6_______6110
7 details inserted
690
7 details inserted
63
7 details inserted
113
7 details inserted
690
7 details inserted
4
7 details inserted
697
7 details inserted
85
7 details inserted
86
7 details inserted
87

7 details inserted
53____689
7 details inserted
600
7 details inserted
36

 

 

7 details inserted
602

7 details inserted
42____2672____636

 

 

7 details inserted
45
7 details inserted
4
7 details inserted
47

________ 648 ________ 649 ________ 650 ________ 651

7 details inserted
6____653____654

7 details inserted
42____2617____636

 

 

7 details inserted
619

7 details inserted
620

đầu ra

Chèn tuple vào bảng sql python

Chúng tôi quan sát thấy bản ghi đó (5,'Thomas','m','information technology') không được chèn lại vào bảng. Hình ảnh sau đây là đầu ra của cơ sở dữ liệu MySQL, sau khi chạy tập lệnh python ở trên

Chèn tuple vào bảng sql python

Chúng ta có thể thấy rằng 0 hàng bị ảnh hưởng. Do đó, không có bản ghi nào được chèn vào vì nó đã tồn tại trong bảng với một bản ghi khác