简体   繁体   中英

How to insert data to postgresql database with flask - sqlalchemy.exc.ProgrammingError?

I would like to insert data to postgresql database with flask.

This is a model class code that I wrote.

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

This is the snippet code which is responsible for inserting data.

@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))

When I run this script, I get an error like this.

sqlalchemy.exc.ProgrammingError

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'builtin_function_or_method'
[SQL: 'INSERT INTO bet365s (id, sports, casino, poker, games_bingo, total, withdrawal, balance) VALUES (%(id)s, %(sports)s, %(casino)s, %(poker)s, %(games_bingo)s, %(total)s, %(withdrawal)s, %(balance)s)']
[parameters: {'withdrawal': 0.0, 'id': <built-in function id>, 'sports': -29.94, 'total': 593.75, 'casino': 464.22, 'poker': 14.29, 'games_bingo': 145.17, 'balance': 593.75}]

I can't find a solution. Please help me.

It looks like the problem is with your column types. You're trying to insert a float into an Integer column.

I wouldn't use id as a class attribute name. You are assigning the id global function to self.id in the initializer which is the cause of the traceback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM