如何使用flask将数据插入postgresql数据库 - sqlalchemy.exc.ProgrammingError?

时间:2017-07-17 17:40:17

标签: python postgresql sqlalchemy

我想将数据插入带有烧瓶的postgresql数据库。

这是我写的模型类代码。

currentUser: User;
constructor(private currentUserService: CurrentUserService) {
     currentUserService.itemValue.subscribe(currentUser => {
     this.currentUser = JSON.parse(currentUser);
    });
}

这是负责插入数据的代码段。

class Bet365(db.Model):
    __tablename__ = "bet365s"
    id = db.Column(db.Integer, primary_key=True)
    sports = db.Column(db.Float)
    casino = db.Column(db.Float)
    poker = db.Column(db.Float)
    games_bingo = db.Column(db.Float)
    total = db.Column(db.Float)
    withdrawal = db.Column(db.Float)
    balance = db.Column(db.Float)

    def __init__(self, sports, casino, poker, games_bingo, total, withdrawal, balance):
        self.id = id
        self.sports = sports
        self.casino = casino
        self.poker = poker
        self.games_bingo = games_bingo
        self.total = total
        self.withdrawal = withdrawal
        self.balance = balance

当我运行此脚本时,我收到这样的错误。

@app.route('/testing/')
def testing():
    data_bet365 = bet365_scrapping()

    sports = float(data_bet365[0])
    casino = float(data_bet365[1])
    poker = float(data_bet365[2])
    games_bingo = float(data_bet365[3])
    total = float(data_bet365[4])
    withdrawal = float(data_bet365[5])
    balance = float(data_bet365[6])

    result_bet365 = Bet365(sports, casino, poker, games_bingo, total, withdrawal, balance)
    db.session.add(result_bet365)
    db.session.commit()
    return (jsonify(data_bet365))

我无法找到解决方案。请帮帮我。

2 个答案:

答案 0 :(得分:0)

看起来问题在于您的列类型。您正在尝试将浮点数插入到Integer列中。

答案 1 :(得分:0)

我不会将id用作类属性名称。您正在初始化程序中将id全局函数分配给self.id,这是回溯的原因。