I have created an app using flask, it takes an input from a user and saves the input into the Postgres Database here's the code:
from flask import Flask, request, render_template, jsonify
import psycopg2 as pg2
import exquixdb
app = Flask(__name__)
@app.route('/questions', methods=["POST"])
def post_question():
request_data = request.json
table = ("INSERT INTO question(id, question) VALUES(%s,%s)", (1003, str(request_data['question'])))
exquixdb.db.create(table)
app.logger.info("adding questions")
return "question saved"
if __name__ == '__main__':
app.run(debug=True, port=8989)
I've created a database Module the code is:
# Module exquizdb.py
import psycopg2 as pg2
class Database():
def db_connect(self, db="exquizdb", user="jim", pas="password", port=5433):
global conn
self.conn = pg2.connect(database=db, user=user, password=pas, port=port)
global cur
self.cur = self.conn.cursor()
def close(self):
self.cur.close()
self.conn.close()
def query(self, query):
self.cur.execute(query)
self.close()
def create(self, table):
self.cur.execute(table)
self.conn.commit()
self.close()
db = Database()
table = ("INSERT INTO question(id, question) VALUES(%s,%s)", (1003, 'Test'))
db.create(table)
so when I run the code I get (AttributeError: 'Database' object has no attribute 'cur') I am new database and python. So i don't know how to fix it. How do I fix the problem. thank you.