Hướng dẫn display postgres table in html - hiển thị bảng postgres trong html

Tôi là người mới ở Python và Sqlalchemy và cần giúp đỡ ở đây. Tôi đang cố gắng hiển thị dữ liệu được lưu trữ trong bảng cơ sở dữ liệu PostgreSQL cho trình duyệt. Tôi không có thông báo lỗi nhưng bảng trên trình duyệt dường như không tìm nạp dữ liệu từ bảng. Tôi không nhận được dữ liệu nào được hiển thị trên trình duyệt nhưng tôi có dữ liệu được lưu trữ trong cơ sở dữ liệu PostgreSQL.

Tôi đang sử dụng SQLalchemy và tôi đã tạo một lớp dữ liệu và hiển thị một bảng trong chế độ xem HTML được cho là hiển thị các hàng bảng từ cơ sở dữ liệu PostgreSQL.

Đây là mã của tôi

from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
import jinja2

SQLALCHEMY_DATABASE_URI ='postgresql://postgres:paswword@localhost/TendersQuotes'
SQLALCHEMY_TRACK_MODIFICATIONS = True

app = Flask(__name__, instance_relative_config=True)
#app.config.from_pyfile('flask.cfg')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:postgres123@localhost/TendersQuotes'
db = SQLAlchemy(app)


class Data(db.Model):
    __tablename__ = "Tenderslist"
    id = db.Column(db.Integer, primary_key=True)
    t_company=db.Column(db.String(120), nullable=False)
    t_title=db.Column(db.String(180), nullable=False)
    t_number=db.Column(db.String(120),nullable=False)
    t_type=db.Column(db.String(120),nullable=False)
    t_status=db.Column(db.String(120),nullable=False)
    t_duedate=db.Column(db.DateTime(),nullable=False)
    t_cperson=db.Column(db.String(120),nullable=False)
    t_cperson_email=db.Column(db.String(120),nullable=False)
    t_cperson_no=db.Column(db.Integer, nullable=False)
    t_comments=db.Column(db.String(120),nullable=False)

    def __init__(self, t_company, t_title, t_number, t_type, t_status, t_duedate, t_cperson, t_cperson_email, t_cperson_no, t_comments):
        self.t_company= t_company
        self.t_title=t_title
        self.t_number=t_number
        self.t_type=t_type
        self.t_status=t_status
        self.t_duedate=t_duedate
        self.t_cperson=t_cperson
        self.t_cperson_email=t_cperson_email
        self.t_cperson_no=t_cperson_no
        self.t_comments=t_comments

@app.route("/")
def index():
    return render_template("index.html")


@app.route("/tenders")
def tenders():
    tenders = Data.query.all()
    return render_template('tenders.html', tenders=tenders)

Sau đó trên trang xem, tôi có mã HTML sau




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}

Có điều gì tôi đang thiếu ở đây không? Coz I Tôi chỉ nhận được một cái đầu của đầu bảng chứ không phải dữ liệu bảng, được cho là được tìm nạp từ cơ sở dữ liệu.

Tôi cần sự hỗ trợ

Hướng dẫn PHP này giúp tạo danh sách HTML bằng cơ sở dữ liệu PostgreSQL. Nó rất đơn giản và dễ dàng để tạo danh sách HTML bằng PHP, giống như các cơ sở dữ liệu khác được sử dụng, ngoại trừ chuỗi kết nối cơ sở dữ liệu PostgreSQL và phương thức Postgres để tìm nạp dữ liệu.

PHP cung cấp các LIB PostgreSQL để truyền đạt PHP với cơ sở dữ liệu Postgres.

Chúng tôi sẽ làm theo các bước sau để tích hợp PostgreSQL với PHP.

  • Bật mô -đun Postgres từ tệp php.ini
  • Tạo chuỗi kết nối bằng cơ sở dữ liệu Postgres.
  • Nhận dữ liệu và hiển thị vào một mẫu HTML.

Hướng dẫn display postgres table in html - hiển thị bảng postgres trong html

Cách bật dữ liệu Postgres trong Php.ini

Chúng tôi sẽ kích hoạt tiện ích mở rộng Postgres từ tệp php.ini, chúng tôi sẽ không trình bày các dòng bên dưới.

uncommented, extension=php_pdo_pgsql.dll,
uncommented, extension=php_pgsql.dll,
uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",

Lưu ý: Bạn có thể đọc Phần thứ hai: JQuery Ajax Thêm, Chỉnh sửa và Xóa bằng Php và PostgreSQL

Cách kết nối cơ sở dữ liệu Postgres với PHP

Chúng tôi sẽ sử dụng phương thức




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
1 để tạo kết nối cơ sở dữ liệu với PostgreSQL. Tôi sẽ vượt qua DBHOST, tên DB, cổng và mật khẩu.

Chúng tôi đã tạo một cơ sở dữ liệu ‘thử nghiệm và tạo nhân viên bằng tập lệnh dưới đây.

CREATE TABLE employee (
    id        char(5),
    employee_name       varchar(100),
    employee_salary         integer,
	employee_age        integer,
    CONSTRAINT code_title PRIMARY KEY(id)
);

Chúng tôi sẽ tạo tệp




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
2 và thêm mã bên dưới,

servername = "localhost";
	  $this->username = "test";
	  $this->password = "test123";
	  $this->dbname = "test";
	  $this->port = "5432";
	}
	function getConnstring() {
		$con = pg_connect("host=".$this->servername." port=".$this->port." dbname=".$this->dbname." user=".$this->username." password=".$this->password."") or die("Connection failed: ".pg_last_error());

		/* check connection */
		if (pg_last_error()) {
			printf("Connect failed: %s\n", pg_last_error());
			exit();
		} else {
			$this->conn = $con;
		}
		return $this->conn;
	}
}
?>

Chúng tôi sẽ tạo tệp




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
3 và thêm mã dưới đây để truy cập các bản ghi dữ liệu từ bảng PostgreSQL.PostgreSQL table.

include("connection.php");
class Employee {
	protected $conn;
	protected $data = array();
	function __construct() {

		$db = new dbObj();
		$connString =  $db->getConnstring();
		$this->conn = $connString;
	}
	
	public function getEmployees() {
		$sql = "SELECT * FROM employee";
		$queryRecords = pg_query($this->conn, $sql) or die("error to fetch employees data");
		$data = pg_fetch_all($queryRecords);
		return $data;
	}
}

Bao gồm tệp




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
2 ở đầu tệp, tệp này được sử dụng để truy cập đối tượng kết nối cơ sở dữ liệu Postgres. Chúng tôi đã xác định lớp nhân viên và gán đối tượng kết nối vào một biến lớp.

Cuối cùng, đã tạo phương thức




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
5 để truy cập dữ liệu nhân viên từ cơ sở dữ liệu Postgres và trả về dữ liệu. Chúng tôi sẽ sử dụng phương thức này sau này vào tệp



  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
6 để đặt kết quả.

Chúng tôi sẽ tạo




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
6 và bao gồm các tệp CSS và JS LIBS cần thiết.


Chúng tôi sẽ nhập tệp




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
3 để truy cập dữ liệu từ Postgres bằng phương thức.

getEmployees();
 ?>

Bây giờ, chúng tôi có tất cả dữ liệu của nhân viên trong biến




  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
9 PHP. Chúng tôi sẽ lặp lại trên mảng



  ....
  




List of Tenders

{% for item in tenders %} {% endfor %}
S/No. Company Tender Title Tender No. Tender Type Status Due Date Contact Person Contact Email Contact Number Comments
{{item[0]}} {{item[1]}} {{item[2]}} {{item[3]}} {{item[4]}} {{item[5]}} {{item[6]}} {{item[7]}} {{item[8]}} {{item[9]}} {{item[10]}}
9 và liên kết dữ liệu với cột Bảng HTML bằng phương pháp
uncommented, extension=php_pdo_pgsql.dll,
uncommented, extension=php_pgsql.dll,
uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",
1.


		$emp)
			

Bạn có thể tải xuống mã nguồn và xem bản demo từ liên kết dưới đây.

Conclusion:

Chúng tôi đã kích hoạt trình điều khiển cơ sở dữ liệu Postgres vào PHP bằng tệp php.ini, cũng đã tạo đối tượng kết nối cơ sở dữ liệu bằng phương pháp

uncommented, extension=php_pdo_pgsql.dll,
uncommented, extension=php_pgsql.dll,
uncommented, extension_dir = "c:/wamp/bin/php/php5.3.5/ext/",
3, lấy dữ liệu từ cơ sở dữ liệu Postgres sử dụng PHP và hiển thị vào bảng HTML.

Làm thế nào tìm nạp dữ liệu từ cơ sở dữ liệu PostgreSQL và hiển thị bảng HTML?

Danh sách bảng HTML bằng cơ sở dữ liệu PHP và PostgreSQL..
Bật mô -đun Postgres từ tệp php.ini ..
Tạo chuỗi kết nối bằng cơ sở dữ liệu Postgres ..
Nhận dữ liệu và hiển thị vào mẫu HTML ..

Làm thế nào kết nối cơ sở dữ liệu PostgreSQL với HTML?

Kết nối PostgreSQL với biểu mẫu HTML với LeadsBridge..
Bước 1: Thông tin chính của Bridge. Chọn một tên cho cây cầu của bạn (điều này sẽ chỉ hiển thị bên trong Leadsbridge) ....
Bước 2: Thiết lập nguồn PostgreSQL của bạn. ....
Bước 3: Thiết lập điểm đến biểu mẫu HTML của bạn. ....
Bước 4: Ánh xạ trường. ....
Bước 5: Kiểm tra ..

Có GUI cho Postgresql không?

PostgreSQL GUI là một công cụ quản lý cho người dùng cơ sở dữ liệu PostgreSQL nguồn mở để truy vấn, trực quan hóa, thao tác, phân tích dữ liệu Postgres.Các công cụ GUI này giúp quản lý triển khai PostgreSQL dễ dàng.Bạn cũng có thể truy cập và điều hướng các máy chủ cơ sở dữ liệu của mình thông qua GUI Postgres.. These GUI tools make administering PostgreSQL deployments easy. You can also access and navigate your database servers via a Postgres GUI.

Làm thế nào tìm nạp dữ liệu từ cơ sở dữ liệu trong bảng HTML và hiển thị HTML?

Làm thế nào chúng ta có thể lấy dữ liệu từ cơ sở dữ liệu và hiển thị ở dạng HTML ?..
Kết nối PHP với cơ sở dữ liệu MySQL.Bạn có thể sử dụng truy vấn kết nối cơ sở dữ liệu sau để kết nối PHP với cơ sở dữ liệu MySQL ..
Chèn dữ liệu vào bảng phpmyadmin ..
Tìm nạp dữ liệu từ bảng MySQL ..
Hiển thị dữ liệu trong bảng HTML ..
Kiểm tra bản thân để chèn dữ liệu ..

Name Salary Age Action