Hướng dẫn create table postgresql in python - tạo bảng postgresql trong python

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 TABLEStatement. 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:

  1. Đầu tiên, xây dựng tạo câu lệnh bảng.
  2. 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àm connect() trả về một đối tượng connection.
  3. Sau đó, tạo đối tượng ________ 8 & nbsp; bằng cách gọi phương thức cursor() của đối tượng connection.
  4. Sau đó, thực hiện CREATE TABLE bằng cách gọi phương thức

    python create_table.py

    Code language: CSS (css)
    2 của đối tượng cursor.
  5. 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

    python create_table.py

    Code language: CSS (css)
    4 của các đối tượng cursorconnection.

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

python create_table.py

Code language: CSS (css)
7.

Thứ hai, bên trong tệp

python create_table.py

Code language: CSS (css)
7, xác định một hàm mới gọi là

python create_table.py

Code language: CSS (css)
9.

Hàm

python create_table.py

Code language: CSS (css)
9 tạo ra bốn bảng trong cơ sở dữ liệu

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)
1:

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)
2,

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)
3,

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)
4 và

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)
5.

Hướng dẫn create table postgresql in python - tạo bảng postgresql trong 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()

Code language: Python (python)

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:

python create_table.py

Code language: CSS (css)

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

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)
6 để hiển thị danh sách bảng từ cơ sở dữ liệu

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)
1.

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)

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

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)
1.

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

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)
9.

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?

Làm cách nào để tạo một bảng trong postgresql?

Bạn có thể tạo một bảng mới bằng cách chỉ định tên bảng, cùng với tất cả các tên cột và các loại của chúng: Tạo thời tiết bảng (Thành phố Varchar (80), Temp_Lo Int, -Nhiệt độ thấp Temp_Hi Int, -PRCP nhiệt độ cao Real, - - Ngày kết tủa ngày); Bạn có thể nhập cái này vào PSQL với các lần ngắt dòng.by specifying the table name, along with all column names and their types: CREATE TABLE weather ( city varchar(80), temp_lo int, -- low temperature temp_hi int, -- high temperature prcp real, -- precipitation date date ); You can enter this into psql with the line breaks.

Làm thế nào để bạn tạo một bảng trong Python?

Cách dễ nhất để tạo các bảng trong Python là sử dụng hàm Table () từ thư viện bảng ...
Để sử dụng chức năng này, trước tiên chúng ta phải cài đặt thư viện bằng PIP: PIP Cài đặt Tabulation ..
Sau đó, chúng ta có thể tải thư viện: từ bảng nhập bảng lập bảng ..

Làm cách nào để tạo cơ sở dữ liệu trong PostgreSQL bằng Python?

Tạo cơ sở dữ liệu bằng Python, bạn có thể tạo đối tượng con trỏ bằng phương thức con trỏ () của lớp kết nối.Phương thức EXECUTE () của lớp này chấp nhận truy vấn PostgreSQL dưới dạng tham số và thực thi nó.Do đó, để tạo cơ sở dữ liệu trong PostgreSQL, hãy thực hiện truy vấn cơ sở dữ liệu tạo bằng phương pháp này.execute the CREATE DATABASE query using this method.

Bạn có thể tạo một bảng SQL trong Python không?

Tạo một bảng.Để tạo một bảng trong MySQL, hãy sử dụng câu lệnh "Tạo bảng".use the "CREATE TABLE" statement.