【问题标题】:Is it possible to pull data from Heroku Postgresql?是否可以从 Heroku Postgresql 中提取数据?
【发布时间】:2022-01-17 03:09:07
【问题描述】:

目前我正在使用 Heroku 和 Flask 开发一个电报机器人来部署它。我想将一些数据存储到数据库中,然后我遇到了 Heroku PostgreSQL。我设法将数据存储到其中,但我无法从 Heroku PostgreSQL 检索这些数据。我进行了很多研究,但找不到有关提取数据过程的太多信息。有人可以启发我或建议使用 Flask 从 Heroku PostgreSQL 检索/提取这些数据的步骤是什么?

Telegram Bot 基本上是一个随机食物生成器。我已将食物列表存储到数据中,并希望在用户触发机器人/动作时提取这些数据和随机生成器。

Screenshot of the current heroku postgresql database

from os import environ
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS
from invokes import invoke_http

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = environ.get("DB_URL") or "postgres://database-url"
app.config["SQLALCHEMY_TRACK_MODIFICATION"] = false

db = SQLAlchemy(app)

CORS(app)

class Cusine(db.Model):
    __tablename__ = "cusines"

    id = db.Column(db.String(255), primary_key =True)
    cusine = db.Column(db.String(255), nullable=False)

    def __init__(self, id, cusine):
        self.id = id
        self.cusine = cusine

    def getId(self):
        return self.id

    def getCusine(self):
        return self.cusine  

    def json(self):
        return {"id": self.id,"cusine":self.cusine}

db.create_all()

@app.route("/getAllCusine")#
def getAllCusine():
    try:
        all_cusine = Cusine.query.all()
        cusines = []
        for row in all_cusine:
            cusines.append(row.cusine)

        return jsonify(
            {
               "code": 200, 
               "message": cusines
            }
        ),200
    except:
        return jsonify(
            {
               "code": 500, 
               "message": "error in getting all course" 
            }
        ),500

那么,如何使用 Flask 从 Heroku PostgreSQL 中提取数据?

提前谢谢你。

【问题讨论】:

  • 你试过什么。通常的方法是演示您遇到的一些示例/错误,这将使社区免于重复相同的事情并在您的工作基础上发展。
  • 其实我还没有真正尝试过,因为我不确定 Heroku PostgreSQL 的过程是什么。因为这是我第一次使用它,关于它的信息有限。
  • “我设法将数据存储到其中,但在检索这些数据时遇到问题”——您如何存储数据?例如,要检索数据,您只需...运行 SELECT 查询而不是 INSERT
  • Please don't post screenshots of text。它们无法被屏幕阅读器等自适应技术的用户搜索或复制,甚至无法使用。相反,将代码作为文本直接粘贴到您的问题中。如果选择它并单击{} 按钮或 Ctrl+K 代码块将缩进四个空格,这将导致其呈现为代码。
  • 嗨,克里斯,感谢您的推荐。很抱歉,这是我第一次在 stackoverflow 上发帖,因此可能对这里的语法不太熟悉。但是我已经直接添加了代码。也就是说,当前存储在数据库中的数据是使用 heroku CLI 手动插入的。

标签: postgresql flask heroku telegram-bot pull-request


【解决方案1】:

我已经设法从 Heroku PostgreSQL 中提取数据。

我已将上面的代码更改为以下内容。

import os
import psycopg2

DATABASE_URL = os.environ['DATABASE_URL']

def retrieveData():
    try:
        conn = psycopg2.connect(DATABASE_URL, sslmode='require')
        cursor = conn.cursor()
        cursor.execute("select * from table") #some sql statement
        conn.commit()
        result = cursor.fetchall()
        cursor.close()

     except (Exception, psycopg2.DatabaseError) as error:
         print(error)

     return result

它将返回一个元组列表。

记得pip install psycopg2-binary

这是我第一次使用 Heroku 和 PostgreSQL,我花了很长时间才弄明白。但是谢谢他教我如何正确使用stackoverflow,谢谢。希望这可以帮助那些不熟悉 PostgreSQL 的人(比如我)。干杯:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多