Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách tạo các bảng mới trong cơ sở dữ liệu PostgreSQL bằng Python.: in this tutorial, you will learn how to create new tables in the PostgreSQL database using Python.
Hướng dẫn này giả định rằng bạn biết cách viết CREATE TABLE
Statement. Nếu đây không phải là trường hợp, bạn nên xem hướng dẫn CREATE TABLE
.
Các bước để tạo bảng PostgreSQL trong Python
Để tạo một bảng mới trong cơ sở dữ liệu PostgreSQL, bạn sử dụng các bước sau:
- Đầu tiên, xây dựng tạo câu lệnh bảng.
- Tiếp theo, kết nối với cơ sở dữ liệu PostgreSQL bằng cách gọi hàm
connect[]
. Hàmconnect[]
trả về một đối tượngconnection
. - Sau đó, tạo đối tượng ________ 8 & nbsp; bằng cách gọi phương thức
cursor[]
của đối tượngconnection
. - Sau đó, thực hiện
CREATE TABLE
bằng cách gọi phương thức
2 của đối tượng
Code language: CSS [css]python create_table.py
cursor
. - Cuối cùng, đóng giao tiếp với máy chủ cơ sở dữ liệu PostgreSQL bằng cách gọi các phương thức
4 của các đối tượng
Code language: CSS [css]python create_table.py
cursor
vàconnection
.
Tạo bảng trong ví dụ Python
1] Tạo chương trình Python
Đầu tiên, tạo một tệp mới có tên
7.Code language: CSS [css]
python create_table.py
Thứ hai, bên trong tệp
7, xác định một hàm mới gọi là
python create_table.py
Code language: CSS [css]
9.Code language: CSS [css]
python create_table.py
Hàm
9 tạo ra bốn bảng trong cơ sở dữ liệuCode language: CSS [css]
python create_table.py
1:Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
2,Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
3,Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
4 vàCode language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
5.
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
Code language: PHP [php]
Code language: Python [python]
#!/usr/bin/python import psycopg2 from config import config def create_tables[]: """ create tables in the PostgreSQL database""" commands = [ """ CREATE TABLE vendors [ vendor_id SERIAL PRIMARY KEY, vendor_name VARCHAR[255] NOT NULL ] """, """ CREATE TABLE parts [ part_id SERIAL PRIMARY KEY, part_name VARCHAR[255] NOT NULL ] """, """ CREATE TABLE part_drawings [ part_id INTEGER PRIMARY KEY, file_extension VARCHAR[5] NOT NULL, drawing_data BYTEA NOT NULL, FOREIGN KEY [part_id] REFERENCES parts [part_id] ON UPDATE CASCADE ON DELETE CASCADE ] """, """ CREATE TABLE vendor_parts [ vendor_id INTEGER NOT NULL, part_id INTEGER NOT NULL, PRIMARY KEY [vendor_id , part_id], FOREIGN KEY [vendor_id] REFERENCES vendors [vendor_id] ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY [part_id] REFERENCES parts [part_id] ON UPDATE CASCADE ON DELETE CASCADE ] """] conn = None try: # read the connection parameters params = config[] # connect to the PostgreSQL server conn = psycopg2.connect[**params] cur = conn.cursor[] # create table one by one for command in commands: cur.execute[command] # close communication with the PostgreSQL database server cur.close[] # commit the changes conn.commit[] except [Exception, psycopg2.DatabaseError] as error: print[error] finally: if conn is not None: conn.close[] if __name__ == '__main__': create_tables[]
2] Thực hiện chương trình Python
Để thực hiện chương trình Python, bạn sử dụng lệnh sau:
Code language: CSS [css]
python create_table.py
3] Xác minh việc tạo bảng
Đầu tiên, đăng nhập vào máy chủ cơ sở dữ liệu PostgreSQL bằng chương trình PSQL.
Thứ hai, sử dụng lệnh
6 để hiển thị danh sách bảng từ cơ sở dữ liệuCode language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
1.Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
Như bạn thấy có thể thấy rõ từ đầu ra, chúng tôi có bốn bảng được tạo thành công trong cơ sở dữ liệu
1.Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
Nếu bạn sử dụng công cụ máy khách khác như PGADMIN, bạn có thể xem các bảng thông qua danh sách bảng trong lược đồ
9.Code language: PHP [php]
suppliers=# \dt List of relations Schema | Name | Type | Owner --------+---------------+-------+---------- public | part_drawings | table | postgres public | parts | table | postgres public | vendor_parts | table | postgres public | vendors | table | postgres [4 rows]
Trong hướng dẫn này, bạn đã học từng bước làm thế nào để tạo các bảng PostgreSQL mới trong Python bằng bộ điều hợp cơ sở dữ liệu PSYCOPG.
Hướng dẫn này có hữu ích không?