Tất cả các chương trình xử lý dữ liệu ở dạng này hay dạng khác và nhiều chương trình cần có khả năng lưu và truy xuất dữ liệu đó từ lần gọi này sang lần gọi tiếp theo. Python, SQLite và SQLAlchemy cung cấp chức năng cơ sở dữ liệu cho chương trình của bạn, cho phép bạn lưu trữ dữ liệu trong một tệp mà không cần máy chủ cơ sở dữ liệu
Bạn có thể đạt được kết quả tương tự bằng cách sử dụng các tệp phẳng ở bất kỳ định dạng nào, bao gồm CSV, JSON, XML và thậm chí cả các định dạng tùy chỉnh. Tệp phẳng thường là tệp văn bản mà con người có thể đọc được—mặc dù chúng cũng có thể là dữ liệu nhị phân—với cấu trúc có thể được phân tích cú pháp bằng chương trình máy tính. Dưới đây, bạn sẽ khám phá cách sử dụng cơ sở dữ liệu SQL và tệp phẳng để lưu trữ và thao tác dữ liệu, đồng thời tìm hiểu cách quyết định phương pháp nào phù hợp với chương trình của bạn
Trong hướng dẫn này, bạn sẽ học cách sử dụng
- Tệp phẳng để lưu trữ dữ liệu
- SQL để cải thiện quyền truy cập vào dữ liệu liên tục
- SQLite để lưu trữ dữ liệu
- SQLAlchemy để làm việc với dữ liệu dưới dạng đối tượng Python
Bạn có thể lấy tất cả mã và dữ liệu mà bạn sẽ thấy trong hướng dẫn này bằng cách nhấp vào liên kết bên dưới
Tải xuống mã mẫu. Nhấp vào đây để lấy mã bạn sẽ sử dụng để tìm hiểu về quản lý dữ liệu với SQLite và SQLAlchemy trong hướng dẫn này
Sử dụng tệp phẳng để lưu trữ dữ liệu
Tệp phẳng là tệp chứa dữ liệu không có cấu trúc phân cấp bên trong và thường không có tham chiếu đến tệp bên ngoài. Các tệp phẳng chứa các ký tự mà con người có thể đọc được và rất hữu ích để tạo và đọc dữ liệu. Bởi vì chúng không phải sử dụng độ rộng trường cố định, các tệp phẳng thường sử dụng các cấu trúc khác để chương trình có thể phân tích cú pháp văn bản
Ví dụ: các tệp giá trị được phân tách bằng dấu phẩy [CSV] là các dòng văn bản thuần túy trong đó ký tự dấu phẩy phân tách các thành phần dữ liệu. Mỗi dòng văn bản đại diện cho một hàng dữ liệu và mỗi giá trị được phân tách bằng dấu phẩy là một trường trong hàng đó. Dấu phân cách ký tự dấu phẩy cho biết ranh giới giữa các giá trị dữ liệu
Python vượt trội trong việc đọc và lưu vào tệp. Khả năng đọc tệp dữ liệu bằng Python cho phép bạn khôi phục ứng dụng về trạng thái hữu ích khi bạn chạy lại ứng dụng sau đó. Khả năng lưu dữ liệu trong một tệp cho phép bạn chia sẻ thông tin từ chương trình giữa người dùng và trang nơi ứng dụng chạy
Trước khi một chương trình có thể đọc tệp dữ liệu, nó phải có khả năng hiểu dữ liệu. Thông thường, điều này có nghĩa là tệp dữ liệu cần phải có một số cấu trúc mà ứng dụng có thể sử dụng để đọc và phân tích cú pháp văn bản trong tệp
Dưới đây là tệp CSV có tên
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6, được sử dụng bởi chương trình ví dụ đầu tiên trong hướng dẫn nàyfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng đầu tiên cung cấp danh sách các trường được phân tách bằng dấu phẩy, là tên cột cho dữ liệu theo sau trong các dòng còn lại. Các dòng còn lại chứa dữ liệu, với mỗi dòng đại diện cho một bản ghi
Ghi chú. Mặc dù tác giả, sách và nhà xuất bản đều có thật nhưng mối quan hệ giữa sách và nhà xuất bản là hư cấu và được tạo ra cho mục đích của hướng dẫn này
Tiếp theo, bạn sẽ xem xét một số ưu điểm và nhược điểm của việc sử dụng các tệp phẳng như CSV ở trên để làm việc với dữ liệu của bạn
Loại bỏ các quảng cáoƯu điểm của tệp phẳng
Làm việc với dữ liệu trong tệp phẳng có thể quản lý và thực hiện dễ dàng. Có dữ liệu ở định dạng con người có thể đọc được không chỉ hữu ích cho việc tạo tệp dữ liệu bằng trình soạn thảo văn bản mà còn để kiểm tra dữ liệu và tìm kiếm bất kỳ sự không nhất quán hoặc vấn đề nào
Nhiều ứng dụng có thể xuất các phiên bản tệp phẳng của dữ liệu do tệp tạo ra. Ví dụ: Excel có thể nhập hoặc xuất tệp CSV sang và từ bảng tính. Các tệp phẳng cũng có ưu điểm là độc lập và có thể chuyển nhượng nếu bạn muốn chia sẻ dữ liệu
Hầu hết mọi ngôn ngữ lập trình đều có các công cụ và thư viện giúp làm việc với các tệp CSV dễ dàng hơn. Python có sẵn mô-đun
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
7 và mô-đun pandas mạnh mẽ, giúp làm việc với các tệp CSV trở thành một giải pháp hiệu quảNhược điểm của tệp phẳng
Ưu điểm của việc làm việc với các tệp phẳng bắt đầu giảm dần khi dữ liệu trở nên lớn hơn. Các tệp lớn vẫn có thể đọc được bằng con người nhưng việc chỉnh sửa chúng để tạo dữ liệu hoặc tìm kiếm sự cố trở thành một nhiệm vụ khó khăn hơn. Nếu ứng dụng của bạn sẽ thay đổi dữ liệu trong tệp, thì một giải pháp sẽ là đọc toàn bộ tệp vào bộ nhớ, thực hiện các thay đổi và ghi dữ liệu ra một tệp khác
Một vấn đề khác khi sử dụng tệp phẳng là bạn sẽ cần tạo và duy trì rõ ràng bất kỳ mối quan hệ nào giữa các phần dữ liệu của bạn và chương trình ứng dụng trong cú pháp tệp. Ngoài ra, bạn sẽ cần tạo mã trong ứng dụng của mình để sử dụng các mối quan hệ đó
Một điều phức tạp cuối cùng là những người bạn muốn chia sẻ tệp dữ liệu của mình cũng sẽ cần biết và hành động dựa trên các cấu trúc và mối quan hệ mà bạn đã tạo trong dữ liệu. Để truy cập thông tin, những người dùng đó không chỉ cần hiểu cấu trúc của dữ liệu mà còn cả các công cụ lập trình cần thiết để truy cập thông tin đó.
Ví dụ tệp phẳng
Chương trình ví dụ
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
8 sử dụng tệp def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6 để lấy dữ liệu và các mối quan hệ trong đó. Tệp CSV này duy trì danh sách tác giả, sách họ đã xuất bản và nhà xuất bản của từng sáchGhi chú. Các tệp dữ liệu được sử dụng trong các ví dụ có sẵn trong thư mục
def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
0. Ngoài ra còn có một tệp chương trình trong thư mục def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
1 tạo dữ liệu. Ứng dụng đó rất hữu ích nếu bạn thay đổi dữ liệu và muốn quay lại trạng thái đã biếtĐể có quyền truy cập vào các tệp dữ liệu được sử dụng trong phần này và trong suốt hướng dẫn, hãy nhấp vào liên kết bên dưới
Tải xuống mã mẫu. Nhấp vào đây để lấy mã bạn sẽ sử dụng để tìm hiểu về quản lý dữ liệu với SQLite và SQLAlchemy trong hướng dẫn này
Tệp CSV trình bày ở trên là một tệp dữ liệu khá nhỏ chỉ chứa một số tác giả, sách và nhà xuất bản. Bạn cũng nên lưu ý một số điều về dữ liệu
Các tác giả Stephen King và Tom Clancy xuất hiện nhiều lần vì nhiều cuốn sách họ đã xuất bản được thể hiện trong dữ liệu
Các tác giả Stephen King và Pearl Buck có cùng một cuốn sách được xuất bản bởi nhiều nhà xuất bản
Các trường dữ liệu trùng lặp này tạo ra mối quan hệ giữa các phần khác của dữ liệu. Một tác giả có thể viết nhiều sách và một nhà xuất bản có thể làm việc với nhiều tác giả. Tác giả và nhà xuất bản chia sẻ mối quan hệ với từng cuốn sách
Các mối quan hệ trong tệp
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6 được thể hiện bằng các trường xuất hiện nhiều lần trong các hàng khác nhau của tệp dữ liệu. Do sự dư thừa dữ liệu này, dữ liệu đại diện cho nhiều hơn một bảng hai chiều. Bạn sẽ thấy nhiều hơn về điều này khi bạn sử dụng tệp để tạo tệp cơ sở dữ liệu SQLiteChương trình ví dụ
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
8 sử dụng các mối quan hệ được nhúng trong tệp def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6 để tạo một số dữ liệu. Đầu tiên nó trình bày một danh sách các tác giả và số lượng sách mà mỗi người đã viết. Sau đó, nó hiển thị danh sách các nhà xuất bản và số lượng tác giả mà mỗi người đã xuất bản sáchNó cũng sử dụng mô-đun
def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
5 để hiển thị hệ thống phân cấp dạng cây của các tác giả, sách và nhà xuất bảnCuối cùng, nó thêm một cuốn sách mới vào dữ liệu và hiển thị lại cấu trúc phân cấp cây với cuốn sách mới tại chỗ. Đây là hàm đầu vào
def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
6 cho chương trình nàyfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
2Mã Python ở trên thực hiện các bước sau
- Dòng 4 đến 7 đọc tệp
6 vào DataFrame của gấu trúcdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
- Dòng 10 đến 13 in số lượng sách xuất bản của mỗi nhà xuất bản
- Dòng 16 đến 19 in số lượng tác giả được liên kết với mỗi nhà xuất bản
- Dòng 22 xuất dữ liệu sách dưới dạng phân cấp được sắp xếp theo tác giả
- Dòng 25 đến 30 thêm một cuốn sách mới vào cấu trúc trong bộ nhớ
- Dòng 33 xuất dữ liệu sách dưới dạng phân cấp được sắp xếp theo tác giả, bao gồm cả sách mới được thêm vào
Chạy chương trình này tạo ra đầu ra sau
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
4Hệ thống phân cấp tác giả ở trên được trình bày hai lần trong đầu ra, với phần bổ sung The Stand của Stephen King, do Random House xuất bản. Đầu ra thực tế ở trên đã được chỉnh sửa và chỉ hiển thị đầu ra phân cấp đầu tiên để tiết kiệm dung lượng
def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
6 gọi các chức năng khác để thực hiện phần lớn công việc. Hàm đầu tiên nó gọi là def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
9def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
Hàm này lấy đường dẫn tệp đến tệp CSV và sử dụng gấu trúc để đọc nó vào DataFrame của gấu trúc, sau đó nó sẽ trả lại cho người gọi. Giá trị trả về của hàm này trở thành cấu trúc dữ liệu được truyền cho các hàm khác tạo nên chương trình
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
80 tính số lượng sách được xuất bản bởi mỗi nhà xuất bản. Sê-ri gấu trúc kết quả sử dụng chức năng GroupBy của gấu trúc để nhóm theo nhà xuất bản và sau đó sắp xếp dựa trên cờ first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
81def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
82 về cơ bản giống như chức năng trước, nhưng đối với tác giảfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
8first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
83 tạo một cuốn sách mới trong DataFrame của gấu trúc. Mã này kiểm tra xem tác giả, cuốn sách hoặc nhà xuất bản đã tồn tại chưa. Nếu không, nó sẽ tạo một cuốn sách mới và nối nó vào DataFrame của gấu trúcdef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
4first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
84 sử dụng các vòng lặp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
85 lồng nhau để lặp qua các cấp của cấu trúc dữ liệu. Sau đó, nó sử dụng mô-đun def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
5 để xuất danh sách phân cấp các tác giả, sách họ đã xuất bản và nhà xuất bản đã xuất bản những cuốn sách đódef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
8Ứng dụng này hoạt động tốt và minh họa sức mạnh có sẵn cho bạn với mô-đun gấu trúc. Mô-đun cung cấp chức năng tuyệt vời để đọc tệp CSV và tương tác với dữ liệu
Hãy tiếp tục và tạo một chương trình hoạt động giống hệt nhau bằng Python, phiên bản cơ sở dữ liệu SQLite của tác giả và dữ liệu xuất bản, và SQLAlchemy để tương tác với dữ liệu đó
Loại bỏ các quảng cáoSử dụng SQLite để duy trì dữ liệu
Như bạn đã thấy trước đó, có dữ liệu dư thừa trong tệp
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6. Ví dụ: tất cả thông tin về Trái đất tốt lành của Pearl Buck được liệt kê hai lần vì hai nhà xuất bản khác nhau đã xuất bản cuốn sáchHãy tưởng tượng nếu tệp dữ liệu này chứa nhiều dữ liệu liên quan hơn, chẳng hạn như địa chỉ và số điện thoại của tác giả, ngày xuất bản và ISBN cho sách hoặc địa chỉ, số điện thoại và có thể là doanh thu hàng năm của nhà xuất bản. Dữ liệu này sẽ được sao chép cho từng mục dữ liệu gốc, chẳng hạn như tác giả, sách hoặc nhà xuất bản
Có thể tạo dữ liệu theo cách này, nhưng nó sẽ cực kỳ khó sử dụng. Hãy suy nghĩ về các vấn đề giữ cho tệp dữ liệu này hiện tại. Nếu Stephen King muốn đổi tên thì sao?
Tồi tệ hơn việc sao chép dữ liệu sẽ là sự phức tạp của việc thêm các mối quan hệ khác vào dữ liệu. Điều gì sẽ xảy ra nếu bạn quyết định thêm số điện thoại của tác giả và họ có số điện thoại nhà riêng, cơ quan, điện thoại di động, v.v.?
Vấn đề này là một lý do mà các mối quan hệ tồn tại trong hệ thống cơ sở dữ liệu. Một chủ đề quan trọng trong kỹ thuật cơ sở dữ liệu là chuẩn hóa cơ sở dữ liệu hoặc quá trình chia nhỏ dữ liệu để giảm dư thừa và tăng tính toàn vẹn. Khi một cấu trúc cơ sở dữ liệu được mở rộng với các loại dữ liệu mới, việc chuẩn hóa nó trước sẽ giữ cho các thay đổi đối với cấu trúc hiện có ở mức tối thiểu
Cơ sở dữ liệu SQLite có sẵn bằng Python và theo trang chủ SQLite, nó được sử dụng nhiều hơn tất cả các hệ thống cơ sở dữ liệu khác cộng lại. Nó cung cấp một hệ thống quản lý cơ sở dữ liệu quan hệ đầy đủ tính năng [RDBMS] hoạt động với một tệp duy nhất để duy trì tất cả các chức năng của cơ sở dữ liệu
Nó cũng có lợi thế là không yêu cầu một máy chủ cơ sở dữ liệu riêng biệt hoạt động. Định dạng tệp cơ sở dữ liệu là đa nền tảng và có thể truy cập được bằng bất kỳ ngôn ngữ lập trình nào hỗ trợ SQLite
Tất cả điều này là thông tin thú vị, nhưng nó liên quan như thế nào đến việc sử dụng các tệp phẳng để lưu trữ dữ liệu?
Tạo cấu trúc cơ sở dữ liệu
Cách tiếp cận mạnh mẽ để lấy dữ liệu
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6 vào cơ sở dữ liệu SQLite sẽ là tạo một bảng duy nhất khớp với cấu trúc của tệp CSV. Làm điều này sẽ bỏ qua rất nhiều sức mạnh của SQLiteCơ sở dữ liệu quan hệ cung cấp cách lưu trữ dữ liệu có cấu trúc trong bảng và thiết lập mối quan hệ giữa các bảng đó. Họ thường sử dụng Ngôn ngữ truy vấn có cấu trúc [SQL] làm cách chính để tương tác với dữ liệu. Đây là sự đơn giản hóa quá mức những gì RDBMS cung cấp, nhưng nó đủ cho mục đích của hướng dẫn này
Cơ sở dữ liệu SQLite cung cấp hỗ trợ để tương tác với bảng dữ liệu bằng SQL. Tệp cơ sở dữ liệu SQLite không chỉ chứa dữ liệu mà còn có một cách tiêu chuẩn hóa để tương tác với dữ liệu. Hỗ trợ này được nhúng trong tệp, nghĩa là bất kỳ ngôn ngữ lập trình nào có thể sử dụng tệp SQLite cũng có thể sử dụng SQL để làm việc với nó
Tương tác với cơ sở dữ liệu bằng SQL
SQL là ngôn ngữ khai báo được sử dụng để tạo, quản lý và truy vấn dữ liệu có trong cơ sở dữ liệu. Một ngôn ngữ khai báo mô tả những gì sẽ được hoàn thành hơn là cách nó nên được hoàn thành. Bạn sẽ thấy các ví dụ về các câu lệnh SQL sau khi bạn bắt đầu tạo các bảng cơ sở dữ liệu
Cấu trúc cơ sở dữ liệu với SQL
Để tận dụng sức mạnh của SQL, bạn sẽ cần áp dụng một số chuẩn hóa cơ sở dữ liệu cho dữ liệu trong tệp
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6. Để làm điều này, bạn sẽ tách tác giả, sách và nhà xuất bản thành các bảng cơ sở dữ liệu riêng biệtVề mặt khái niệm, dữ liệu được lưu trữ trong cơ sở dữ liệu trong cấu trúc bảng hai chiều. Mỗi bảng bao gồm các hàng bản ghi và mỗi bản ghi bao gồm các cột hoặc trường chứa dữ liệu
Dữ liệu chứa trong các trường thuộc các loại được xác định trước, bao gồm văn bản, số nguyên, số float, v.v. Tệp CSV khác vì tất cả các trường đều là văn bản và phải được chương trình phân tích cú pháp để gán loại dữ liệu cho chúng
Mỗi bản ghi trong bảng có một khóa chính được xác định để cung cấp cho bản ghi một mã định danh duy nhất. Khóa chính tương tự như khóa trong từ điển Python. Bản thân công cụ cơ sở dữ liệu thường tạo khóa chính dưới dạng giá trị số nguyên tăng dần cho mọi bản ghi được chèn vào bảng cơ sở dữ liệu
Mặc dù khóa chính thường được tạo tự động bởi công cụ cơ sở dữ liệu, nhưng nó không nhất thiết phải. Nếu dữ liệu được lưu trữ trong một trường là duy nhất trên tất cả các dữ liệu khác trong bảng trong trường đó, thì đó có thể là khóa chính. Ví dụ: bảng chứa dữ liệu về sách có thể sử dụng ISBN của sách làm khóa chính
Loại bỏ các quảng cáoTạo bảng với SQL
Đây là cách bạn có thể tạo ba bảng đại diện cho tác giả, sách và nhà xuất bản trong tệp CSV bằng câu lệnh SQL
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
2Lưu ý rằng không có thao tác tệp nào, không có biến nào được tạo và không có cấu trúc nào để giữ chúng. Các câu lệnh chỉ mô tả kết quả mong muốn. việc tạo một bảng với các thuộc tính cụ thể. Công cụ cơ sở dữ liệu xác định cách thực hiện việc này
Khi bạn đã tạo và điền vào bảng này dữ liệu tác giả từ tệp
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6, bạn có thể truy cập bảng này bằng các câu lệnh SQL. Câu lệnh sau [còn gọi là truy vấn] sử dụng ký tự đại diện [def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
41] để lấy tất cả dữ liệu trong bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và xuất radef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6Bạn có thể sử dụng công cụ dòng lệnh
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
43 để tương tác với tệp cơ sở dữ liệu def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44 trong thư mục def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
0first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
20Khi công cụ dòng lệnh SQLite đang chạy với cơ sở dữ liệu đang mở, bạn có thể nhập các lệnh SQL. Đây là lệnh SQL ở trên và đầu ra của nó, theo sau là lệnh
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
46 để thoát khỏi chương trìnhfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
21Lưu ý rằng mỗi tác giả chỉ tồn tại một lần trong bảng. Không giống như tệp CSV có nhiều mục cho một số tác giả, ở đây, chỉ cần một bản ghi duy nhất cho mỗi tác giả
Duy trì cơ sở dữ liệu với SQL
SQL cung cấp các cách để làm việc với cơ sở dữ liệu và bảng hiện có bằng cách chèn dữ liệu mới và cập nhật hoặc xóa dữ liệu hiện có. Đây là một câu lệnh SQL ví dụ để chèn một tác giả mới vào bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
22Câu lệnh SQL này chèn các giá trị ‘
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
48‘ và ‘def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
49‘ vào các cột tương ứng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
80 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
81 của bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42Lưu ý rằng cột
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83 không được chỉ định. Bởi vì cột đó là khóa chính, công cụ cơ sở dữ liệu tạo ra giá trị và chèn nó như một phần của việc thực thi câu lệnhCập nhật bản ghi trong bảng cơ sở dữ liệu là một quá trình không phức tạp. Chẳng hạn, giả sử Stephen King muốn được biết đến với bút danh Richard Bachman. Đây là câu lệnh SQL để cập nhật bản ghi cơ sở dữ liệu
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
23Câu lệnh SQL định vị bản ghi đơn cho
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
84 bằng cách sử dụng câu lệnh điều kiện def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
85, sau đó cập nhật các trường def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
80 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
81 với các giá trị mới. SQL sử dụng dấu bằng [def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
88] vừa là toán tử so sánh vừa là toán tử gánBạn cũng có thể xóa bản ghi khỏi cơ sở dữ liệu. Đây là một câu lệnh SQL ví dụ để xóa một bản ghi khỏi bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
24Câu lệnh SQL này xóa một hàng khỏi bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 trong đó def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
80 bằng với def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
22 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
81 bằng với def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
24Cẩn thận khi xóa hồ sơ. Các điều kiện bạn đặt phải càng cụ thể càng tốt. Một điều kiện quá rộng có thể dẫn đến việc xóa nhiều bản ghi hơn bạn dự định. Ví dụ: nếu điều kiện chỉ dựa trên dòng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
25, thì tất cả các tác giả có tên là Paul sẽ bị xóa khỏi cơ sở dữ liệuGhi chú. Để tránh vô tình xóa bản ghi, nhiều ứng dụng hoàn toàn không cho phép xóa. Thay vào đó, bản ghi có một cột khác để cho biết liệu nó có đang được sử dụng hay không. Cột này có thể được đặt tên là
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
26 và chứa một giá trị đánh giá là Đúng hoặc Sai, cho biết liệu bản ghi có nên được đưa vào khi truy vấn cơ sở dữ liệu hay khôngVí dụ: truy vấn SQL bên dưới sẽ lấy tất cả các cột cho tất cả các bản ghi đang hoạt động trong
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
27first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
25SQLite không có kiểu dữ liệu Boolean, vì vậy cột
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
26 được biểu thị bằng một số nguyên có giá trị def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
29 hoặc def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
60 để biểu thị trạng thái của bản ghi. Các hệ thống cơ sở dữ liệu khác có thể có hoặc không có kiểu dữ liệu Boolean gốcHoàn toàn có thể xây dựng các ứng dụng cơ sở dữ liệu bằng Python bằng cách sử dụng các câu lệnh SQL trực tiếp trong mã. Làm như vậy sẽ trả về dữ liệu cho ứng dụng dưới dạng danh sách danh sách hoặc danh sách từ điển
Sử dụng SQL thô là một cách hoàn toàn có thể chấp nhận được để làm việc với dữ liệu do truy vấn cơ sở dữ liệu trả về. Tuy nhiên, thay vì làm điều đó, bạn sẽ chuyển thẳng sang sử dụng SQLAlchemy để làm việc với cơ sở dữ liệu
Loại bỏ các quảng cáoXây dựng các mối quan hệ
Một tính năng khác của hệ thống cơ sở dữ liệu mà bạn có thể thấy thậm chí còn mạnh mẽ và hữu ích hơn tính bền vững và truy xuất dữ liệu là các mối quan hệ. Cơ sở dữ liệu hỗ trợ các mối quan hệ cho phép bạn chia nhỏ dữ liệu thành nhiều bảng và thiết lập kết nối giữa chúng
Dữ liệu trong tệp
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6 thể hiện dữ liệu và mối quan hệ bằng cách sao chép dữ liệu. Cơ sở dữ liệu xử lý việc này bằng cách chia nhỏ dữ liệu thành ba bảng—def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42, def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64—và thiết lập mối quan hệ giữa chúngSau khi thu thập tất cả dữ liệu bạn muốn vào một nơi trong tệp CSV, tại sao bạn lại muốn chia dữ liệu đó thành nhiều bảng?
Mối quan hệ một-nhiều
Mối quan hệ một-nhiều giống như mối quan hệ của một khách hàng đặt hàng trực tuyến. Một khách hàng có thể có nhiều đơn hàng, nhưng mỗi đơn hàng thuộc về một khách hàng. Cơ sở dữ liệu
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44 có mối quan hệ một-nhiều dưới dạng tác giả và sách. Mỗi tác giả có thể viết nhiều sách, nhưng mỗi sách do một tác giả viếtNhư bạn đã thấy trong phần tạo bảng ở trên, việc triển khai các thực thể riêng biệt này là đặt từng thực thể vào một bảng cơ sở dữ liệu, một dành cho tác giả và một dành cho sách. Nhưng làm cách nào để thực hiện mối quan hệ một-nhiều giữa hai bảng này?
Hãy nhớ rằng, mỗi bảng trong cơ sở dữ liệu có một trường được chỉ định làm khóa chính cho bảng đó. Mỗi bảng ở trên có một trường khóa chính được đặt tên bằng mẫu này.
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
66Bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 hiển thị ở trên chứa một trường, def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83, tham chiếu đến bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42. Trường def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83 thiết lập mối quan hệ một-nhiều giữa tác giả và sách giống như thế nàySơ đồ trên là một sơ đồ mối quan hệ thực thể [ERD] đơn giản được tạo bằng ứng dụng JetBrains DataGrip hiển thị các bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 dưới dạng các hộp có khóa chính và trường dữ liệu tương ứng của chúng. Hai mục đồ họa thêm thông tin về mối quan hệCác biểu tượng phím nhỏ màu vàng và xanh dương lần lượt biểu thị khóa chính và khóa ngoại của bảng
Mũi tên kết nối
63 vớidef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
42 biểu thị mối quan hệ giữa các bảng dựa trên khóa ngoạidef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
83 trong bảngdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
63def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Khi bạn thêm một cuốn sách mới vào bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63, dữ liệu bao gồm giá trị def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83 cho tác giả hiện có trong bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42. Bằng cách này, tất cả các cuốn sách được viết bởi một tác giả đều có mối quan hệ tra cứu về tác giả duy nhất đóBây giờ bạn có các bảng riêng biệt cho tác giả và sách, làm thế nào để bạn sử dụng mối quan hệ giữa chúng?
Truy vấn SQL bên dưới kết hợp bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 bằng ứng dụng dòng lệnh SQLitefirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
26Truy vấn SQL ở trên thu thập thông tin từ cả bảng tác giả và sách bằng cách nối các bảng bằng mối quan hệ được thiết lập giữa hai bảng. Nối chuỗi SQL gán tên đầy đủ của tác giả cho bí danh
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
213. Dữ liệu được thu thập bởi truy vấn được sắp xếp theo thứ tự tăng dần theo trường def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
81Có một vài điều cần chú ý trong câu lệnh SQL. Đầu tiên, các tác giả được trình bày theo tên đầy đủ của họ trong một cột duy nhất và được sắp xếp theo họ của họ. Ngoài ra, các tác giả xuất hiện trong đầu ra nhiều lần do mối quan hệ một-nhiều. Tên của tác giả được sao chép cho mỗi cuốn sách họ đã viết trong cơ sở dữ liệu
Bằng cách tạo các bảng riêng cho tác giả và sách, đồng thời thiết lập mối quan hệ giữa chúng, bạn đã giảm được sự dư thừa trong dữ liệu. Giờ đây, bạn chỉ phải chỉnh sửa dữ liệu của tác giả ở một nơi và thay đổi đó sẽ xuất hiện trong mọi truy vấn SQL truy cập dữ liệu
Mối quan hệ nhiều-nhiều
Mối quan hệ nhiều-nhiều tồn tại trong cơ sở dữ liệu
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44 giữa tác giả và nhà xuất bản cũng như giữa sách và nhà xuất bản. Một tác giả có thể làm việc với nhiều nhà xuất bản và một nhà xuất bản có thể làm việc với nhiều tác giả. Tương tự, một cuốn sách có thể được xuất bản bởi nhiều nhà xuất bản và một nhà xuất bản có thể xuất bản nhiều cuốn sáchXử lý tình huống này trong cơ sở dữ liệu liên quan nhiều hơn mối quan hệ một-nhiều vì mối quan hệ có cả hai chiều. Mối quan hệ nhiều-nhiều được tạo bởi một bảng kết hợp đóng vai trò là cầu nối giữa hai bảng có liên quan
Bảng liên kết chứa ít nhất hai trường khóa ngoại, là khóa chính của mỗi trong hai bảng được liên kết. Câu lệnh SQL này tạo bảng liên kết liên quan đến bảng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
27Các câu lệnh SQL tạo một bảng
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 mới tham chiếu các khóa chính của các bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64 hiện có. Bảng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 là bảng liên kết thiết lập mối quan hệ giữa tác giả và nhà xuất bảnVì mối quan hệ là giữa hai khóa chính nên không cần tạo khóa chính cho chính bảng liên kết. Sự kết hợp của hai khóa liên quan sẽ tạo ra một mã định danh duy nhất cho một hàng dữ liệu
Như trước đây, bạn sử dụng từ khóa
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
210 để kết nối hai bảng với nhau. Kết nối bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 với bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64 là quy trình gồm hai bước
210 bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
42 với bảngdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
218first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
210 bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
218 với bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
64def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Bảng liên kết
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 cung cấp cầu nối qua đó first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
210 kết nối hai bảng. Đây là một truy vấn SQL ví dụ trả về danh sách các tác giả và nhà xuất bản xuất bản sách của họfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
28Các câu lệnh trên thực hiện các hành động sau
Dòng 1 bắt đầu một câu lệnh
233 để lấy dữ liệu từ cơ sở dữ liệufirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 2 chọn họ và tên từ bảng
42 bằng cách sử dụng bí danhdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
235 cho bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
42 và nối chúng lại với nhau bằng ký tự khoảng trắngdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 3 chọn tên nhà xuất bản có bí danh là
237first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 4 sử dụng bảng
42 làm nguồn đầu tiên để truy xuất dữ liệu và gán nó cho bí danhdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
235first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 5 là bước đầu tiên của quy trình được nêu ở trên để kết nối bảng
42 với bảngdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
64. Nó sử dụng bí danhdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
242 cho bảng liên kếtfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
218 và thực hiện thao tácfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
210 để kết nối tham chiếu khóa ngoạifirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
245 với khóa chínhfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
246 trong bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
42def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 6 là bước thứ hai trong quy trình hai bước nêu trên. Nó sử dụng bí danh
248 cho bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
64 và thực hiện thao tácdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
210 để liên kết tham chiếu khóa ngoạifirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
251 với khóa chínhfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
252 trong bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
64def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 7 sắp xếp dữ liệu theo họ của tác giả theo thứ tự bảng chữ cái tăng dần và kết thúc truy vấn SQL
Dòng 8 đến 17 là đầu ra của truy vấn SQL
Lưu ý rằng dữ liệu trong các bảng nguồn
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64 được chuẩn hóa, không có dữ liệu trùng lặp. Tuy nhiên, kết quả trả về có dữ liệu trùng lặp khi cần thiết để trả lời truy vấn SQLTruy vấn SQL ở trên trình bày cách sử dụng mối quan hệ bằng cách sử dụng từ khóa SQL
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
210, nhưng dữ liệu kết quả là sự tạo lại một phần của dữ liệu CSV def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
6. Lợi ích của việc hoàn thành công việc tạo cơ sở dữ liệu để phân tách dữ liệu là gì?Đây là một truy vấn SQL khác để thể hiện một chút sức mạnh của SQL và công cụ cơ sở dữ liệu
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
29Truy vấn SQL ở trên trả về danh sách tác giả và số lượng sách họ đã viết. Danh sách được sắp xếp đầu tiên theo số lượng sách giảm dần, sau đó theo tên tác giả theo thứ tự bảng chữ cái
Dòng 1 bắt đầu truy vấn SQL với từ khóa
233first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 2 chọn họ và tên tác giả, cách nhau bởi ký tự khoảng trắng và tạo bí danh
213first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 3 đếm số sách do mỗi tác giả viết, sau này mệnh đề
260 sẽ dùng để sắp xếp danh sáchfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 4 chọn bảng
42 để lấy dữ liệu và tạo bí danhdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
235first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 5 kết nối với bảng
63 có liên quan thông quadef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
210 đếnfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
83 và tạo bí danhdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
266 cho bảngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
63def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 6 tạo dữ liệu tổng hợp về tác giả và tổng số sách bằng cách sử dụng từ khóa
268.first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
268 là những gì nhóm mỗifirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
213 và kiểm soát những cuốn sách đượcfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
271 kiểm đếm cho tác giả đófirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 7 sắp xếp đầu ra đầu tiên theo số lượng sách theo thứ tự giảm dần, sau đó theo họ của tác giả theo thứ tự bảng chữ cái tăng dần
Dòng 8 đến 14 là đầu ra của truy vấn SQL
Trong ví dụ trên, bạn tận dụng SQL để thực hiện tính toán tổng hợp và sắp xếp kết quả theo thứ tự hữu ích. Để cơ sở dữ liệu thực hiện các phép tính dựa trên khả năng tổ chức dữ liệu tích hợp của nó thường nhanh hơn so với việc thực hiện các loại phép tính tương tự trên các tập dữ liệu thô trong Python. SQL cung cấp những lợi thế của việc sử dụng lý thuyết tập hợp được nhúng trong cơ sở dữ liệu RDBMS
Loại bỏ các quảng cáoSơ đồ mối quan hệ thực thể
Sơ đồ mối quan hệ thực thể [ERD] là một mô tả trực quan về mô hình mối quan hệ thực thể cho cơ sở dữ liệu hoặc một phần của cơ sở dữ liệu. Cơ sở dữ liệu SQLite
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44 đủ nhỏ để toàn bộ cơ sở dữ liệu có thể được hiển thị bằng sơ đồ hiển thị bên dướiSơ đồ này trình bày các cấu trúc bảng trong cơ sở dữ liệu và các mối quan hệ giữa chúng. Mỗi hộp đại diện cho một bảng và chứa các trường được xác định trong bảng, với khóa chính được chỉ định trước nếu nó tồn tại
Các mũi tên hiển thị mối quan hệ giữa các bảng kết nối một trường khóa ngoại trong một bảng với một trường, thường là khóa chính, trong một bảng khác. Bảng
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
273 có hai mũi tên, một mũi tên kết nối nó với bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 và một mũi tên khác kết nối nó với bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64. Mũi tên biểu thị mối quan hệ nhiều-nhiều giữa các bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64. Bảng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 cung cấp mối quan hệ tương tự giữa def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64Làm việc với các đối tượng SQLAlchemy và Python
SQLAlchemy là bộ công cụ truy cập cơ sở dữ liệu mạnh mẽ dành cho Python, với trình ánh xạ quan hệ đối tượng [ORM] là một trong những thành phần nổi tiếng nhất của nó và là thành phần được thảo luận và sử dụng tại đây
Khi bạn đang làm việc với một ngôn ngữ hướng đối tượng như Python, sẽ rất hữu ích khi nghĩ về các đối tượng. Có thể ánh xạ các kết quả được trả về bởi các truy vấn SQL tới các đối tượng, nhưng làm như vậy sẽ đi ngược lại bản chất của cách thức hoạt động của cơ sở dữ liệu. Gắn bó với các kết quả vô hướng do SQL cung cấp hoạt động ngược lại với cách các nhà phát triển Python làm việc. Vấn đề này được gọi là trở kháng quan hệ đối tượng không phù hợp
ORM do SQLAlchemy cung cấp nằm giữa cơ sở dữ liệu SQLite và chương trình Python của bạn và biến đổi luồng dữ liệu giữa công cụ cơ sở dữ liệu và các đối tượng Python. SQLAlchemy cho phép bạn suy nghĩ về các đối tượng và vẫn giữ được các tính năng mạnh mẽ của một công cụ cơ sở dữ liệu
Ngươi mâu
Một trong những yếu tố cơ bản để cho phép kết nối SQLAlchemy với cơ sở dữ liệu là tạo một mô hình. Mô hình là một lớp Python xác định ánh xạ dữ liệu giữa các đối tượng Python được trả về do truy vấn cơ sở dữ liệu và các bảng cơ sở dữ liệu bên dưới
Sơ đồ mối quan hệ thực thể được hiển thị trước đó hiển thị các hộp được kết nối với các mũi tên. Các hộp là các bảng được tạo bằng các lệnh SQL và là thứ mà các lớp Python sẽ mô hình hóa. Các mũi tên là mối quan hệ giữa các bảng
Các mô hình là các lớp Python kế thừa từ một lớp SQLAlchemy
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
281. Lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
281 cung cấp các hoạt động giao diện giữa các thể hiện của mô hình và bảng cơ sở dữ liệuDưới đây là tệp
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
283 tạo các mô hình để biểu diễn cơ sở dữ liệu def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
40Đây là những gì đang diễn ra trong mô-đun này
Dòng 1 nhập các lớp
285,first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
286,first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
287,first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
288 vàfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
289 từ SQLAlchemy, được sử dụng để giúp xác định các thuộc tính mô hìnhfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 2 nhập các đối tượng
290 vàfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
291, được sử dụng để tạo mối quan hệ giữa các đối tượngfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 3 nhập đối tượng
292, đối tượng kết nối công cụ cơ sở dữ liệu với chức năng SQLAlchemy của các mô hìnhfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 5 tạo lớp
281, đây là thứ mà tất cả các mô hình kế thừa từ đó và cách chúng nhận được chức năng SQLAlchemy ORMfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Các dòng 7 đến 12 tạo mô hình bảng liên kết
218first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 14 đến 19 tạo mô hình bảng liên kết
273first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 21 đến 29 xác định mô hình lớp
296 cho bảng cơ sở dữ liệufirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
42def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 31 đến 38 xác định mô hình lớp
298 cho bảng cơ sở dữ liệufirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
63def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Các dòng 40 đến 49 xác định mô hình lớp
400 cho bảng cơ sở dữ liệufirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
64def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Mô tả ở trên cho thấy ánh xạ của năm bảng trong cơ sở dữ liệu
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44. Nhưng nó che đậy một số tính năng SQLAlchemy ORM, bao gồm first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
289, first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288, first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
290 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
291. Bây giờ chúng ta hãy đi vào những điều đófirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
289 Tạo liên kết
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
273 đều là các phiên bản của lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
289 tạo ra các bảng liên kết nhiều-nhiều được sử dụng giữa các bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64 và các bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64 tương ứngLớp SQLAlchemy
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
289 tạo một thể hiện duy nhất của bảng được ánh xạ ORM trong cơ sở dữ liệu. Tham số đầu tiên là tên bảng như được xác định trong cơ sở dữ liệu và tham số thứ hai là first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
416, cung cấp kết nối giữa chức năng SQLAlchemy và công cụ cơ sở dữ liệuPhần còn lại của các tham số là các phiên bản của lớp
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
285 xác định các trường bảng theo tên, loại của chúng và trong ví dụ trên, một phiên bản của first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288Loại bỏ các quảng cáofirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288 Tạo kết nối
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
Lớp SQLAlchemy
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288 xác định sự phụ thuộc giữa hai trường first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
285 trong các bảng khác nhau. Một first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288 là cách bạn làm cho SQLAlchemy nhận biết được mối quan hệ giữa các bảng. Ví dụ: dòng này từ quá trình tạo cá thể first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 thiết lập mối quan hệ khóa ngoạifirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
41Câu lệnh trên cho SQLAlchemy biết rằng có một cột trong bảng
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 có tên là def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83. Loại cột đó là first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
286 và def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83 là khóa ngoại liên quan đến khóa chính trong bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42Có cả
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
430 được xác định trong trường hợp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
289 sẽ tạo kết nối từ bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 đến bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
64 và ngược lại, thiết lập mối quan hệ nhiều-nhiềufirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
290 Thành lập Bộ sưu tập
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
Có một
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288 xác định sự tồn tại của mối quan hệ giữa các bảng nhưng không phải là bộ sưu tập sách mà một tác giả có thể có. Hãy xem dòng này trong định nghĩa lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
42Đoạn mã trên định nghĩa một bộ sưu tập cha-con. Thuộc tính
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
438 ở dạng số nhiều [không phải là một yêu cầu, chỉ là một quy ước] là một dấu hiệu cho thấy đó là một bộ sưu tậpTham số đầu tiên của
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
290, tên lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 [không phải là tên bảng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63], là lớp mà thuộc tính first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
438 có liên quan. first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
443 thông báo cho SQLAlchemy rằng có một mối quan hệ giữa các lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298. SQLAlchemy sẽ tìm mối quan hệ trong định nghĩa lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
43SQLAlchemy nhận ra rằng đây là điểm kết nối
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
288 giữa hai lớp. Bạn sẽ nhận được tham số first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
291 trong first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
290 trong giây látMối quan hệ khác trong
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 là với lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
400. Điều này được tạo bằng câu lệnh sau trong định nghĩa lớp first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
44Giống như
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
438, thuộc tính first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
454 chỉ ra một tập hợp các nhà xuất bản được liên kết với một tác giả. Tham số đầu tiên, first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
455, thông báo cho SQLAlchemy lớp liên quan là gì. Tham số thứ hai và thứ ba là first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
456 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
457
458 nói với SQLAlchemy rằng mối quan hệ với lớpfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
400 là thông qua một bảng phụ, đó là bảng liên kếtfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
218 được tạo trước đó trongfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
283. Tham sốfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
458 làm cho SQLAlchemy tìm thấyfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
430first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
288 được xác định trong bảng liên kếtfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
218first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
466 là một cấu hình thuận tiện cho SQLAlchemy biết rằng có một bộ sưu tập bổ sung trong lớpfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
400 có tên làfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
468first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
291 Thuộc tính gương
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
Tham số
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
291 của bộ sưu tập first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
438 first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
290 tạo thuộc tính def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 cho mỗi phiên bản first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298. Thuộc tính này đề cập đến cha mẹ first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 mà phiên bản first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 có liên quan đếnVí dụ: nếu bạn thực thi đoạn mã Python sau, thì một phiên bản
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 sẽ được trả về từ truy vấn SQLAlchemy. Phiên bản first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 có các thuộc tính có thể được sử dụng để in ra thông tin về cuốn sáchfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
45Sự tồn tại của thuộc tính
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 trong first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 ở trên là do định nghĩa của first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
291. Một first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
291 có thể rất hữu ích khi bạn cần tham chiếu đến cha mẹ và tất cả những gì bạn có là một phiên bản conLoại bỏ các quảng cáoCâu hỏi Trả lời câu hỏi
Bạn có thể tạo một truy vấn cơ bản như
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
483 trong SQLAlchemy như thế nàyfirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
46first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
484 là một đối tượng SQLAlchemy được sử dụng để giao tiếp với SQLite trong các chương trình mẫu Python. Tại đây, bạn nói với phiên bạn muốn thực hiện truy vấn đối với mô hình first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 và trả về tất cả các bản ghiTại thời điểm này, những lợi thế của việc sử dụng SQLAlchemy thay vì SQL thuần túy có thể không rõ ràng, đặc biệt khi xem xét thiết lập cần thiết để tạo các mô hình đại diện cho cơ sở dữ liệu.
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
486 được trả về bởi truy vấn là nơi điều kỳ diệu xảy ra. Thay vì nhận lại danh sách danh sách dữ liệu vô hướng, bạn sẽ nhận lại danh sách các phiên bản của đối tượng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 có thuộc tính khớp với tên cột bạn đã xác địnhCác bộ sưu tập
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
438 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
454 do SQLAlchemy duy trì tạo ra một danh sách phân cấp các tác giả và những cuốn sách họ đã viết cũng như các nhà xuất bản đã xuất bản chúngĐằng sau hậu trường, SQLAlchemy biến các cuộc gọi đối tượng và phương thức thành các câu lệnh SQL để thực thi đối với công cụ cơ sở dữ liệu SQLite. SQLAlchemy biến đổi dữ liệu được trả về bởi các truy vấn SQL thành các đối tượng Python
Với SQLAlchemy, bạn có thể thực hiện truy vấn tổng hợp phức tạp hơn được hiển thị trước đó cho danh sách tác giả và số lượng sách họ đã viết như thế này
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
47Truy vấn trên nhận được họ và tên của tác giả, cùng với số lượng sách mà tác giả đã viết. Tổng hợp
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
490 được sử dụng bởi mệnh đề first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
491 dựa trên họ của tác giả. Cuối cùng, các kết quả được sắp xếp theo thứ tự giảm dần dựa trên tổng hợp và bí danh first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
492Chương trình ví dụ
Chương trình ví dụ
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
493 có cùng chức năng như def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
8 nhưng chỉ sử dụng SQLAlchemy để giao tiếp với cơ sở dữ liệu SQLite của def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
44. Chương trình được chia thành hàm def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
6 và các hàm mà nó gọifirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
48Chương trình này là phiên bản sửa đổi của
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
8. Hãy vượt qua sự khác biệtCác dòng 4 đến 7 trước tiên khởi tạo biến
498 thành đường dẫn tệp cơ sở dữ liệu. Sau đó, họ tạo biếnfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
499 để giao tiếp với SQLite và tệp cơ sở dữ liệufirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
44, là điểm truy cập cơ sở dữ liệu của SQLAlchemydef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 8 tạo lớp
01 từ lớpdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
02 của SQLAlchemydef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 9 liên kết
01 với công cụ được tạo ở dòng 8def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 10 tạo phiên bản
484, được chương trình sử dụng để giao tiếp với SQLAlchemyfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Phần còn lại của hàm tương tự, ngoại trừ việc thay thế
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
05 bằng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
484 làm tham số đầu tiên cho tất cả các hàm được gọi bởi def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
6first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
80 đã được tái cấu trúc để sử dụng SQLAlchemy và các mô hình bạn đã xác định trước đó để nhận dữ liệu được yêu cầufirst_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
49Đây là chức năng mới,
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
09, đang làmDòng 6 tạo biến
10 và đặt nó bằng hàm SQLAlchemydef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
11 hoặcdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
12 tùy thuộc vào giá trị của tham sốdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
81first_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Các dòng 9 đến 11 truy vấn bảng
400 để trả về dữ liệu, trong trường hợp này làfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
15 và tổng cộng của các đối tượngdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
298 được liên kết với một tác giả, có bí danh làfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
17def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 12 tham gia vào bộ sưu tập
18def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 13 tổng hợp số lượng sách theo thuộc tính
15def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 14 sắp xếp đầu ra theo số lượng sách theo toán tử được xác định bởi
10def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 15 đóng đối tượng, thực hiện truy vấn và trả về kết quả cho người gọi
Tất cả các đoạn mã trên thể hiện những gì được mong muốn hơn là cách nó được truy xuất. Bây giờ thay vì sử dụng SQL để mô tả những gì bạn muốn, bạn đang sử dụng các đối tượng và phương thức Python. Những gì được trả về là một danh sách các đối tượng Python thay vì một danh sách các bộ dữ liệu
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
82 cũng đã được sửa đổi để hoạt động độc quyền với SQLAlchemy. Chức năng của nó rất giống với chức năng trước đó, vì vậy phần mô tả chức năng bị bỏ quadef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
0def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
22 đã được thêm vào để có danh sách các tác giả được sắp xếp theo họ của họ. Kết quả của truy vấn này là một danh sách các đối tượng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 chứa một bộ sách. Các đối tượng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 đã chứa dữ liệu phân cấp nên không cần phải định dạng lại kết quảdef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
1Giống như phiên bản trước,
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
83 tương đối phức tạp nhưng dễ hiểu. Nó xác định xem một cuốn sách có cùng tiêu đề, tác giả và nhà xuất bản đã tồn tại trong cơ sở dữ liệu chưaNếu truy vấn tìm kiếm tìm thấy kết quả khớp chính xác, thì hàm sẽ trả về. Nếu không có cuốn sách nào phù hợp với tiêu chí tìm kiếm chính xác, thì nó sẽ tìm kiếm xem liệu tác giả đã viết một cuốn sách bằng cách sử dụng tiêu đề đã nhập chưa. Mã này tồn tại để ngăn các sách trùng lặp được tạo trong cơ sở dữ liệu
Nếu không có cuốn sách phù hợp nào tồn tại và tác giả chưa viết cuốn sách nào có cùng tiêu đề thì một cuốn sách mới sẽ được tạo. Sau đó, chức năng truy xuất hoặc tạo tác giả và nhà xuất bản. Khi các phiên bản của
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298, first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
296 và first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
400 tồn tại, mối quan hệ giữa chúng được tạo và thông tin kết quả được lưu vào cơ sở dữ liệudef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
2Đoạn mã trên tương đối dài. Hãy chia chức năng thành các phần có thể quản lý được
Các dòng 7 đến 18 đặt biến
63 thành một phiên bản củadef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
298 nếu tìm thấy một cuốn sách có cùng tên, tác giả và nhà xuất bản. Mặt khác, họ đặtfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
63 thànhdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
32def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 20 và 21 xác định xem sách đã tồn tại chưa và trả về nếu có
Các dòng 24 đến 37 đặt biến
63 thành một phiên bản củadef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
298 nếu tìm thấy một cuốn sách có cùng tên và tác giả. Mặt khác, họ tạo một phiên bảnfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
298 mớifirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Các dòng 40 đến 52 đặt biến
42 thành tác giả hiện có, nếu tìm thấy hoặc tạo một phiên bảndef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
296 mới dựa trên tên tác giả được truyền vàofirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 55 đến 63 đặt biến
64 thành nhà xuất bản hiện có, nếu tìm thấy hoặc tạo phiên bảndef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
400 mới dựa trên tên nhà xuất bản được truyền vàofirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 66 đặt phiên bản
40 thành phiên bảndef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
42. Điều này tạo ra mối quan hệ giữa tác giả và cuốn sách mà SQLAlchemy sẽ tạo trong cơ sở dữ liệu khi phiên được cam kếtdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 67 thêm phiên bản
64 vào bộ sưu tậpdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
43. Điều này tạo ra mối quan hệ nhiều-nhiều giữa các bảngdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
63 vàdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
64. SQLAlchemy sẽ tạo các tham chiếu trong các bảng cũng như trong bảng liên kếtdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
273 kết nối haifirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 68 thêm phiên bản
298 vào phiên, làm cho nó trở thành một phần của đơn vị công việc của phiênfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Dòng 71 cam kết tất cả các sáng tạo và cập nhật vào cơ sở dữ liệu
Có một vài điều cần lưu ý ở đây. Đầu tiên, không có đề cập đến bảng liên kết
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
218 hoặc first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
273 trong truy vấn hoặc tạo và cập nhật. Do công việc bạn đã làm trong first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
283 khi thiết lập các mối quan hệ, SQLAlchemy có thể xử lý việc kết nối các đối tượng với nhau và giữ các bảng đó đồng bộ trong quá trình tạo và cập nhậtThứ hai, tất cả các sáng tạo và cập nhật xảy ra trong ngữ cảnh của đối tượng
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
484. Không có hoạt động nào chạm vào cơ sở dữ liệu. Chỉ khi câu lệnh def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
52 thực thi thì phiên đó mới đi qua đơn vị công việc của nó và cam kết công việc đó với cơ sở dữ liệuVí dụ: nếu một phiên bản
first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 mới được tạo [như trong dòng 37 ở trên], thì cuốn sách có các thuộc tính được khởi tạo ngoại trừ khóa chính def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
54 và khóa ngoại def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83. Bởi vì chưa có hoạt động cơ sở dữ liệu nào xảy ra, nên def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
54 không xác định và không có gì được thực hiện trong quá trình khởi tạo của def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 để cung cấp cho nó một def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83Khi
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
52 được thực thi, một trong những việc nó sẽ làm là chèn def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
63 vào cơ sở dữ liệu, lúc này cơ sở dữ liệu sẽ tạo khóa chính def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
54. Sau đó, phiên sẽ khởi tạo giá trị def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
62 với giá trị khóa chính được tạo bởi công cụ cơ sở dữ liệudef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
52 cũng biết về việc chèn phiên bản first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 vào bộ sưu tập def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
65. Khóa chính def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83 của đối tượng def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
42 sẽ được thêm vào đối tượng first_name,last_name,title,publisher
Isaac,Asimov,Foundation,Random House
Pearl,Buck,The Good Earth,Random House
Pearl,Buck,The Good Earth,Simon & Schuster
Tom,Clancy,The Hunt For Red October,Berkley
Tom,Clancy,Patriot Games,Simon & Schuster
Stephen,King,It,Random House
Stephen,King,It,Penguin Random House
Stephen,King,Dead Zone,Random House
Stephen,King,The Shining,Penguin Random House
John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley
Alex,Michaelides,The Silent Patient,Simon & Schuster
Carol,Shaben,Into The Abyss,Simon & Schuster
298 được thêm vào bộ sưu tập def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
65 dưới dạng khóa ngoại def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
83Loại bỏ các quảng cáoCung cấp quyền truy cập cho nhiều người dùng
Đến thời điểm này, bạn đã biết cách sử dụng pandas, SQLite và SQLAlchemy để truy cập cùng một dữ liệu theo nhiều cách khác nhau. Đối với trường hợp sử dụng tương đối đơn giản của dữ liệu tác giả, sách và nhà xuất bản, việc bạn có nên sử dụng cơ sở dữ liệu hay không vẫn có thể là một vấn đề khó khăn.
Một yếu tố quyết định khi lựa chọn giữa việc sử dụng tệp phẳng hoặc cơ sở dữ liệu là độ phức tạp của dữ liệu và mối quan hệ. Nếu dữ liệu cho mỗi thực thể phức tạp và chứa nhiều mối quan hệ giữa các thực thể thì việc tạo và duy trì dữ liệu đó trong một tệp phẳng có thể trở nên khó khăn hơn
Một yếu tố khác cần xem xét là liệu bạn có muốn chia sẻ dữ liệu giữa nhiều người dùng hay không. Giải pháp cho vấn đề này có thể đơn giản như sử dụng sneakernet để di chuyển dữ liệu vật lý giữa những người dùng. Di chuyển tệp dữ liệu theo cách này có ưu điểm là dễ sử dụng, nhưng dữ liệu có thể nhanh chóng không đồng bộ khi thực hiện thay đổi
Vấn đề giữ dữ liệu nhất quán cho tất cả người dùng càng trở nên khó khăn hơn nếu người dùng ở xa và muốn truy cập dữ liệu qua các mạng. Ngay cả khi bạn bị giới hạn ở một ngôn ngữ duy nhất như Python và sử dụng gấu trúc để truy cập dữ liệu, khóa tệp mạng không đủ để đảm bảo dữ liệu không bị hỏng
Cung cấp dữ liệu thông qua ứng dụng máy chủ và giao diện người dùng giúp giảm bớt vấn đề này. Máy chủ là ứng dụng duy nhất cần quyền truy cập cấp tệp vào cơ sở dữ liệu. Bằng cách sử dụng cơ sở dữ liệu, máy chủ có thể tận dụng SQL để truy cập dữ liệu bằng giao diện nhất quán cho dù máy chủ sử dụng ngôn ngữ lập trình nào
Chương trình ví dụ cuối cùng chứng minh điều này bằng cách cung cấp một ứng dụng web và giao diện người dùng cho cơ sở dữ liệu SQLite mẫu Chinook. Peter Stark hào phóng duy trì cơ sở dữ liệu Chinook như một phần của trang Hướng dẫn SQLite. Nếu bạn muốn tìm hiểu thêm về SQLite và SQL nói chung, thì trang web này là một nguồn tài nguyên tuyệt vời
Cơ sở dữ liệu Chinook cung cấp thông tin về nghệ sĩ, âm nhạc và danh sách phát dọc theo dòng của một Spotify đơn giản hóa. Cơ sở dữ liệu là một phần của dự án mã ví dụ trong thư mục
def get_books_by_publisher[data, ascending=True]:
"""Return the number of books by each publisher as a pandas series"""
return data.groupby["publisher"].size[].sort_values[ascending=ascending]
0Sử dụng Flask với Python, SQLite và SQLAlchemy
Chương trình
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
72 tạo ứng dụng Flask mà bạn có thể tương tác bằng trình duyệt. Ứng dụng này sử dụng các công nghệ sauFlask Blueprint là một phần của Flask và cung cấp một cách tốt để tuân theo nguyên tắc thiết kế tách biệt các mối quan tâm và tạo các mô-đun riêng biệt để chứa chức năng
Flask SQLAlchemy là một phần mở rộng cho Flask bổ sung hỗ trợ cho SQLAlchemy trong các ứng dụng web của bạn
Flask_Bootstrap4 đóng gói bộ công cụ ngoại vi Bootstrap, tích hợp nó với các ứng dụng web Flask của bạn
Flask_WTF mở rộng Flask với WTForms, cung cấp cho các ứng dụng web của bạn một cách hữu ích để tạo và xác thực các biểu mẫu web
python_dotenv là một mô-đun Python mà một ứng dụng sử dụng để đọc các biến môi trường từ một tệp và giữ thông tin nhạy cảm ra khỏi mã chương trình
Mặc dù không cần thiết cho ví dụ này, tệp
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
73 chứa các biến môi trường cho ứng dụng. Tệp def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
73 tồn tại để chứa thông tin nhạy cảm như mật khẩu mà bạn nên tránh xa các tệp mã của mình. Tuy nhiên, nội dung của tệp dự án def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
73 được hiển thị bên dưới vì nó không chứa bất kỳ dữ liệu nhạy cảm nàodef get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
3Ứng dụng ví dụ khá lớn và chỉ một số ứng dụng có liên quan đến hướng dẫn này. Vì lý do này, việc kiểm tra và học hỏi từ mã được để lại như một bài tập cho người đọc. Điều đó nói rằng, bạn có thể xem ảnh chụp màn hình động của ứng dụng bên dưới, theo sau là HTML hiển thị trang chủ và tuyến Python Flask cung cấp dữ liệu động
Đây là ứng dụng đang hoạt động, điều hướng qua các menu và tính năng khác nhau
Chụp màn hình động bắt đầu trên trang chủ của ứng dụng, được tạo kiểu bằng Bootstrap 4. Trang hiển thị các nghệ sĩ trong cơ sở dữ liệu, được sắp xếp theo thứ tự tăng dần. Phần còn lại của ảnh chụp màn hình trình bày kết quả của việc nhấp vào các liên kết được hiển thị hoặc điều hướng xung quanh ứng dụng từ menu cấp cao nhất
Đây là mẫu HTML Jinja2 tạo trang chủ của ứng dụng
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
4Đây là những gì đang diễn ra trong mã mẫu Jinja2 này
Dòng 1 sử dụng kế thừa mẫu Jinja2 để xây dựng mẫu này từ mẫu
76. Mẫudef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
76 chứa tất cả mã soạn sẵn HTML5 cũng như thanh điều hướng Bootstrap nhất quán trên tất cả các trang của trang webdef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 3 đến 37 chứa nội dung khối của trang, được tích hợp vào macro Jinja2 cùng tên trong mẫu cơ sở
76def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Các dòng 9 đến 13 hiển thị biểu mẫu để tạo một nghệ sĩ mới. Điều này sử dụng các tính năng của Flask-WTF để tạo biểu mẫu
Dòng 24 đến 32 tạo vòng lặp
85 hiển thị bảng tên nghệ sĩfirst_name,last_name,title,publisher Isaac,Asimov,Foundation,Random House Pearl,Buck,The Good Earth,Random House Pearl,Buck,The Good Earth,Simon & Schuster Tom,Clancy,The Hunt For Red October,Berkley Tom,Clancy,Patriot Games,Simon & Schuster Stephen,King,It,Random House Stephen,King,It,Penguin Random House Stephen,King,Dead Zone,Random House Stephen,King,The Shining,Penguin Random House John,Le Carre,"Tinker, Tailor, Soldier, Spy: A George Smiley Novel",Berkley Alex,Michaelides,The Silent Patient,Simon & Schuster Carol,Shaben,Into The Abyss,Simon & Schuster
Các dòng 27 đến 29 hiển thị tên nghệ sĩ dưới dạng liên kết đến trang album của nghệ sĩ hiển thị các bài hát được liên kết với một nghệ sĩ cụ thể
Đây là lộ trình Python hiển thị trang
def get_data[filepath]:
"""Get book data from the csv file"""
return pd.read_csv[filepath]
5Hãy xem đoạn mã trên đang làm gì
Các dòng 1 đến 6 nhập tất cả các mô-đun cần thiết để hiển thị trang và khởi tạo biểu mẫu với dữ liệu từ cơ sở dữ liệu
Dòng 9 đến 11 tạo bản thiết kế cho trang nghệ sĩ
Dòng 13 đến 20 tạo chức năng trình xác thực tùy chỉnh cho biểu mẫu Flask-WTF để đảm bảo yêu cầu tạo nghệ sĩ mới không xung đột với nghệ sĩ hiện có
Các dòng 22 đến 25 tạo lớp biểu mẫu để xử lý biểu mẫu nghệ sĩ được hiển thị trong trình duyệt và cung cấp xác thực các đầu vào của trường biểu mẫu
Các dòng 27 đến 28 kết nối hai tuyến đường với chức năng
80 mà chúng trang trídef get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 30 tạo một thể hiện của lớp
81def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 33 xác định xem trang có được yêu cầu thông qua phương thức HTTP GET hoặc POST [gửi]. Nếu đó là POST, thì nó cũng xác thực các trường của biểu mẫu và thông báo cho người dùng nếu các trường không hợp lệ
Các dòng 35 đến 37 tạo một đối tượng nghệ sĩ mới, thêm nó vào phiên SQLAlchemy và cam kết đối tượng nghệ sĩ vào cơ sở dữ liệu, duy trì nó
Dòng 38 chuyển hướng trở lại trang nghệ sĩ, trang này sẽ được hiển thị lại với nghệ sĩ mới được tạo
Dòng 40 chạy truy vấn SQLAlchemy để lấy tất cả các nghệ sĩ trong cơ sở dữ liệu và sắp xếp chúng theo
82def get_data[filepath]: """Get book data from the csv file""" return pd.read_csv[filepath]
Dòng 41 hiển thị trang nghệ sĩ nếu phương thức yêu cầu HTTP là GET
Bạn có thể thấy rằng rất nhiều chức năng được tạo ra bởi một lượng mã khá nhỏ
Loại bỏ các quảng cáoTạo một máy chủ API REST
Bạn cũng có thể tạo một máy chủ web cung cấp API REST. Loại máy chủ này cung cấp các điểm cuối URL phản hồi dữ liệu, thường ở định dạng JSON. Một máy chủ cung cấp các điểm cuối API REST có thể được sử dụng bởi các ứng dụng web một trang JavaScript thông qua việc sử dụng các yêu cầu AJAX HTTP
Flask là một công cụ tuyệt vời để tạo các ứng dụng REST. Đối với loạt bài hướng dẫn gồm nhiều phần về cách sử dụng Flask, Connexion và SQLAlchemy để tạo các ứng dụng REST, hãy xem API REST của Python với Flask, Connexion và SQLAlchemy
Nếu bạn là người hâm mộ Django và quan tâm đến việc tạo API REST, thì hãy xem Django Rest Framework – Giới thiệu và tạo API REST siêu cơ bản với Django Tastypie
Ghi chú. Thật hợp lý khi hỏi liệu SQLite có phải là lựa chọn phù hợp làm cơ sở dữ liệu phụ trợ cho ứng dụng web hay không. Trang web SQLite tuyên bố rằng SQLite là một lựa chọn tốt cho các trang web phục vụ khoảng 100.000 lượt truy cập mỗi ngày. Nếu trang web của bạn nhận được nhiều lượt truy cập hàng ngày hơn, điều đầu tiên phải nói là xin chúc mừng
Ngoài ra, nếu bạn đã triển khai trang web của mình bằng SQLAlchemy, thì có thể di chuyển dữ liệu từ SQLite sang cơ sở dữ liệu khác như MySQL hoặc PostgreSQL. Để so sánh SQLite, MySQL và PostgreSQL sẽ giúp bạn đưa ra quyết định xem cái nào sẽ phục vụ ứng dụng của bạn tốt nhất, hãy xem Giới thiệu về Thư viện Python SQL
Rất đáng để xem xét SQLite cho ứng dụng Python của bạn, bất kể nó là gì. Việc sử dụng cơ sở dữ liệu mang lại tính linh hoạt cho ứng dụng của bạn và nó có thể tạo ra những cơ hội đáng ngạc nhiên để thêm các tính năng bổ sung
Phần kết luận
Bạn đã nắm được rất nhiều kiến thức trong hướng dẫn này về cơ sở dữ liệu, SQLite, SQL và SQLAlchemy. Bạn đã sử dụng những công cụ này để di chuyển dữ liệu chứa trong tệp phẳng sang cơ sở dữ liệu SQLite, truy cập dữ liệu bằng SQL và SQLAlchemy, đồng thời cung cấp dữ liệu đó thông qua máy chủ web
Trong hướng dẫn này, bạn đã học
- Tại sao cơ sở dữ liệu SQLite có thể là giải pháp thay thế hấp dẫn cho lưu trữ dữ liệu tệp phẳng
- Cách chuẩn hóa dữ liệu để giảm dư thừa dữ liệu và tăng tính toàn vẹn của dữ liệu
- Cách sử dụng SQLAlchemy để làm việc với cơ sở dữ liệu theo hướng đối tượng
- Cách xây dựng ứng dụng web để phục vụ cơ sở dữ liệu cho nhiều người dùng
Làm việc với cơ sở dữ liệu là một khái niệm trừu tượng mạnh mẽ để làm việc với dữ liệu bổ sung chức năng quan trọng cho các chương trình Python của bạn và cho phép bạn đặt các câu hỏi thú vị về dữ liệu của mình
Bạn có thể lấy tất cả mã và dữ liệu bạn đã xem trong hướng dẫn này tại liên kết bên dưới
Tải xuống mã mẫu. Nhấp vào đây để lấy mã bạn sẽ sử dụng để tìm hiểu về quản lý dữ liệu với SQLite và SQLAlchemy trong hướng dẫn này
Đọc thêm
Hướng dẫn này là phần giới thiệu về cách sử dụng cơ sở dữ liệu, SQL và SQLAlchemy, nhưng còn nhiều điều nữa để tìm hiểu về các chủ đề này. Đây là những công cụ tinh vi, mạnh mẽ mà không một hướng dẫn đơn lẻ nào có thể trình bày đầy đủ. Dưới đây là một số tài nguyên để biết thêm thông tin nhằm mở rộng kỹ năng của bạn
Nếu ứng dụng của bạn sẽ hiển thị cơ sở dữ liệu cho người dùng, thì việc tránh các cuộc tấn công SQL injection là một kỹ năng quan trọng. Để biết thêm thông tin, hãy xem Ngăn chặn các cuộc tấn công SQL injection với Python
Cung cấp quyền truy cập web vào cơ sở dữ liệu là phổ biến trong các ứng dụng một trang dựa trên web. Để tìm hiểu cách thực hiện, hãy xem API REST của Python với Flask, Connexion và SQLAlchemy - Phần 2
Chuẩn bị cho các cuộc phỏng vấn công việc kỹ thuật dữ liệu giúp bạn thăng tiến trong sự nghiệp. Để bắt đầu, hãy xem Câu hỏi phỏng vấn kỹ sư dữ liệu với Python
Di chuyển dữ liệu và có thể khôi phục bằng cách sử dụng Flask với Postgres và SQLAlchemy là một phần không thể thiếu trong Vòng đời phát triển phần mềm [SDLC]. Bạn có thể tìm hiểu thêm về nó bằng cách xem Flask by Example – Thiết lập Postgres, SQLAlchemy và Alembic
Đánh dấu là đã hoàn thành
Xem ngay Hướng dẫn này có một khóa học video liên quan do nhóm Real Python tạo. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn. SQLite và SQLAlchemy trong Python. Di chuyển dữ liệu của bạn ra ngoài tệp phẳng
🐍 Thủ thuật Python 💌
Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python
Gửi cho tôi thủ thuật Python »
Giới thiệu về Doug Farrell
Doug là nhà phát triển Python với hơn 25 năm kinh nghiệm. Anh ấy viết về Python trên trang web cá nhân của mình và làm việc với tư cách là Kỹ sư web cấp cao của Shutterfly
» Thông tin thêm về DougMỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là
Aldren
Geir Arne
Joanna
Gia-cốp
Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia
Nâng cao kỹ năng Python của bạn »
Bậc thầy Kỹ năng Python trong thế giới thực
Với quyền truy cập không giới hạn vào Python thực
Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia
Nâng cao kỹ năng Python của bạn »
Bạn nghĩ sao?
Đánh giá bài viết này
Tweet Chia sẻ Chia sẻ EmailBài học số 1 hoặc điều yêu thích mà bạn đã học được là gì?
Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi