I am fresh to flask and was trying to build a server on my own, and I ran into an issue with SQLite operation error.
I did not make model.py (only running with app.py), do i have to make it?
Here's my code and Error
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
import json
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'crud.sqlite')
db = SQLAlchemy(app)
ma = Marshmallow(app)
class Info(db.Model):
id = db.Column(db.Integer, primary_key = True)
camId = db.Column(db.Integer, unique = False)
location = db.Column(db.String(80), unique=False)
isActive = db.Column(db.Integer, unique = False)
imgUrl = db.Column(db.String(120), unique= False)
gender = db.Column(db.String(80), unique=False)
age = db.Column(db.Integer, unique = False)
def __init__(self, camId, location, isActive, imgUrl, gender, age):
self.camId = camId
self.location = location
self.isActive = isActive
self.imgUrl = imgUrl
self.gender = gender
self.age = age
class InfoSchema(ma.Schema):
class Meta:
fields = ('camId', 'location', 'isActive', 'imgUrl', 'gender', 'age')
info_schema = InfoSchema()
info_schemas = InfoSchema(many=True)
#app.route('/')
def base():
# return (basedir)
for jsonfile in os.listdir(basedir+"/dbex/"):
if jsonfile[-4:] == "json":
with open(basedir+"/dbex/"+jsonfile) as data_result:
data = json.load(data_result)
return jsonify(data)
else:
return 'Nothing found'
#app.route('/api/create/cameraData', methods =["POST"])
def create_cameraData():
content = request.json
camId = content["camId"]
location = content["location"]
isActive = content["isActive"]
imgUrl = content["imgUrl"]
gender = content["gender"]
age = content["age"]
new_data = Info(camId, location, isActive, imgUrl, gender, age)
db.session.add(new_data)
db.session.commit()
return "success"
#app.route("/api/get/all", methods=["GET"])
def get_all_camera():
all_camera = Info.query.all()
result = info_schemas.dump(all_camera)
return jsonify(result.data)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: info [SQL: 'SELECT info.id AS info_id, info."camId" AS "info_camId", info.location AS info_location, info."isActive" AS "info_isActive", info."imgUrl" AS "info_imgUrl", info.gender AS info_gender, info.age AS info_age \nFROM info'] (Background on this error at: http://sqlalche.me/e/e3q8)
Related
I have code that converts a csv into an sqlalchemy table. When I try and use db.session.add() it creates an entry in the datatable, but the id is null. This stops me from being able to interact with the data. I tried creating a normal SQL alchemy table and was able to insert rows fine, but for whatever reason the way pandas added the table is preventing me from adding more rows to the table. I will attach the relevant code. Let me know if you need more
db = SQLAlchemy()
DB_NAME = "database.db"
app = Flask(__name__)
sslify = SSLify(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db.init_app(app)
class Master(db.Model):
__tablename__ = "Master"
__table_args__ = {'extend_existing':True}
index = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(150))
full_name = db.Column(db.String(150))
serial_number = db.Column(db.String(150))
asset_tag = db.Column(db.Integer)
def __repr__(self):
return '<Comments %r>' % self.id
def __init__(self, username, full_name, serial_number, asset_tag):
self.username = username
self.full_name = full_name
self.serial_number = serial_number
self.asset_tag = asset_tag
file_name = 'master.csv'
df = pd.read_csv(file_name)
df['asset_tag'].fillna('-999', inplace=True)
df['asset_tag'] = df['asset_tag'].astype('int')
df.replace(-999," ",inplace=True)
df.fillna(" ", inplace=True)
df.to_sql(con = app.config['SQLALCHEMY_DATABASE_URI'], index_label='index', name=Master.__tablename__, if_exists='append')
#app.route('/adds', methods = ['GET','POST'])
def adds():
thefullname = request.form.get('add_full')
theuser = request.form.get('add_user')
theasset = request.form.get('add_asset')
theserial = request.form.get('add_serial')
theadd = Master(username = theuser, full_name=thefullname, asset_tag=theasset, serial_number=theserial)
db.session.add(theadd)
db.session.commit()
return render_template('home.html')
I am trying to create a endpoint to return data from two tables indicator and metadata so I created the model below with indicator = db.relationship('Indicator', backref='metadatas') and indicator_id = db.Column(db.Integer, db.ForeignKey('indicator.id')) but when I call the api/indicators I get empty json as you can see at the end, why?
models.py
from datetime import datetime
from app import db, ma
class Indicator(db.Model):
id = db.Column(db.Integer, primary_key=True)
unique_key = db.Column(db.String(32))
name = db.Column(db.String(255))
short_name = db.Column(db.String(255))
valid = db.Column(db.Boolean)
created_at = db.Column(db.DateTime())
last_updated_at = db.Column(db.DateTime())
class Metadata(db.Model):
id = db.Column(db.Integer, primary_key=True)
indicator_id = db.Column(db.Integer, db.ForeignKey('indicator.id'))
indicator = db.relationship('Indicator', backref='metadatas')
type = db.Column(db.String(255))
inputs = db.Column(db.String(255))
options = db.Column(db.String(255))
outputs = db.Column(db.String(255))
class IndicatorSchema(ma.SQLAlchemySchema):
class Meta:
model = Indicator
id: ma.auto_field()
unique_key: ma.auto_field()
name: ma.auto_field()
short_name: ma.auto_field()
valid: ma.auto_field()
class MetadataSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Metadata
include_fk = True
Below you can see the other files:
app.py
from flask_jwt_extended import JWTManager
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "fe7e8955db51c0ff78550419434128cb"
app.config["JWT_ACCESS_TOKEN_EXPIRES "] = 28800
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////tmp/test.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['JSON_SORT_KEYS'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
controller.py
from flask_jwt_extended import jwt_required, create_access_token, get_jwt_identity
from flask import Flask, request, jsonify
from models import Indicator, Metadata, IndicatorSchema, MetadataSchema
from config import conn_string
from app import app, db
import services
import hashlib
import json
#app.route('/api/indicators', methods=['GET'])
#jwt_required()
def indicators():
short_name = request.args.get('short_name',None)
if short_name is not None:
indicator = Indicator.query.filter_by(short_name = short_name).first()
indicator_schema = IndicatorSchema()
output = indicator_schema.dump(indicator)
else:
indicators = Indicator.query.all()
indicators_schema = IndicatorSchema(many=True)
output = indicators_schema.dump(indicators)
return jsonify(output), 200
When I call the /api/indicators I get response 200 but empty JSON:
$ http :5000/api/indicators
HTTP/1.0
Content-Type: application/json
Server: Werkzeug/2.0.0 Python/3.8.5
[
{},
{},
{},
{},
{},
{}
]
I think there are two issues:
You should be defining the relationship on the Indicator model (one indicator with many metadatas)
I believe you're looking for the meta attribute include_relationships, since you're looking to return more than just the foreign key ids
Your code with the adjustments:
from datetime import datetime
from app import db, ma
class Indicator(db.Model):
__tablename__ = "indicator"
id = db.Column(db.Integer, primary_key=True)
unique_key = db.Column(db.String(32))
name = db.Column(db.String(255))
short_name = db.Column(db.String(255))
valid = db.Column(db.Boolean)
created_at = db.Column(db.DateTime())
last_updated_at = db.Column(db.DateTime())
metadatas = db.relationship('Metadata', backref='indicator')
class Metadata(db.Model):
__tablename__ = "metadata"
id = db.Column(db.Integer, primary_key=True)
indicator_id = db.Column(db.Integer, db.ForeignKey('indicator.id'))
type = db.Column(db.String(255))
inputs = db.Column(db.String(255))
options = db.Column(db.String(255))
outputs = db.Column(db.String(255))
class IndicatorSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Indicator
include_relationships = True
id: ma.auto_field()
unique_key: ma.auto_field()
name: ma.auto_field()
short_name: ma.auto_field()
valid: ma.auto_field()
class MetadataSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = Metadata
I want to store the details stored in x variable to the sqlite database using flask sqlalchemy. How to make it possible.
Here's the code i wrote:
from flask import Flask
from flask_httpauth import HTTPBasicAuth
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/u.db'
db = SQLAlchemy(app)
class User(db.Model) :
x = ['username = "sam"', 'password = "sam123"']
u1 = (x[0].split()[0])
p1 = (x[1].split()[0])
print(u1,p1)
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key = True)
u1 = db.Column(db.String(32), index = True)
p1 = db.Column(db.String(128))
if __name__ == '__main__':
db.create_all()
print("db created")
app.run(host='0.0.0.0', port=5001)
table created in sqlite:
id u1 p1
Required table to be created in sqlite and data to be loaded:
id username password
1 sam sam123
Your table needs to define the columns with the names that you want:
class User(db.Model) :
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(32), index = True)
password = db.Column(db.String(128))
You can make function to extract the username and password from x:
def get_user_data(data):
user_data = []
for item in data:
part = item.partition(' = ')[2]
cleaned = part.replace('"', '')
user_data.append(cleaned)
return user_data
And create a User instance like this:
username, password = get_user_data(x)
user = User(username=username, password=password)
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
#import sqlite3 as sql
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://ahmad:ahmad#192.168.3.103/utama'
db = SQLAlchemy(app)
class ak(db.Model):
__tablename__ = 'ak'
id = db.Column(db.Integer, primary_key=True)
nama = db.Column(db.String)
alamat = db.Column(db.String)
akreditasi = db.Column(db.String)
def __init__(self, id, nama, alamat, akreditasi):
self.id = id
self.city = nama
self.alamat = alamat
self.akreditasi = akreditasi
class av(db.Model):
__tablename__ = 'av'
id = db.Column(db.Integer, primary_key=True)
nama = db.Column(db.String)
alamat = db.Column(db.String)
akreditasi = db.Column(db.String)
def __init__(self, id, nama, alamat, akreditasi):
self.id = id
self.city = nama
self.alamat = alamat
self.akreditasi = akreditasi
id_jurusan = db.Table('id_jurusan',
db.Column('id', db.Integer, db.ForeignKey('ak.id')),
db.Column('id', db.Integer, db.ForeignKey('av.id'))
)
#app.route('/ak')
def jurusan(jurusan):
return render_template('index.html', rows=ak.query.all() )
#app.route('/av')
def Akuntansi():
return render_template('index.html', rows=av.query.all() )
if __name__ == '__main__':
app.run(debug=True, host='1.1.1.1', port=80)
I am a new to learn python, in this case I studied the framework flask and I had trouble on the declaration SQLAlchemy, precisely displays the contents of the table but with the same structure,when executed will be like this.....
[
which one success
You are using the decorator
#app.route('/av')
The method which succeeds Akuntansi() does not require a parameter. So this works. The method which fails expects a parameter jurusan(jurusan) but your decorator #app.route('/ak') does not consider this.
To pass a parameter you need to use the decorator like this:
#app.route("/ak/<jurusan>") and then also pass the parameter in the request.
Am new to python and am usign marshmallow serialization. unable to use the nested scehma.
, my code
from sqlalchemy import Column, Float, Integer, String, Text, text,ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
Base = declarative_base()
metadata = Base.metadata
class CompanyDemo(Base):
__tablename__ = 'company_demo'
company_id = Column(Integer, primary_key=True,
server_default=text("nextval('company_demo_company_id_seq'::regclass)"))
name = Column(Text, nullable=False)
address = Column(String(50))
location = Column(String(50))
class UsersDemo(Base):
__tablename__ = 'users_demo'
id = Column(Integer, primary_key=True,
server_default=text("nextval('users_demo_id_seq'::regclass)"))
company_id = Column(Integer,ForeignKey('company_demo.company_id'), nullable=False)
email = Column(String)
company = relationship('CompanyDemo')
schema
from marshmallow import Schema, fields, pprint
class CompanySchema(Schema):
company_id = fields.Int(dump_only=True)
name = fields.Str()
address = fields.Str()
location = fields.Str()
class UserSchema(Schema):
email = fields.Str()
company = fields.Nested(CompanySchema)
user = UserSchema()
user = UserSchema(many=True)
company = CompanySchema()
company = CompanySchema(many=True)
and my flask app
from flask import Flask, jsonify, url_for, render_template
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from flask_sqlalchemy import SQLAlchemy
from model import CompanyDemo, UsersDemo
from schemas.userschema import user, company
app = Flask(__name__)
app.secret_key = "shiva"
def db_connect():
engine = create_engine('postgresql://ss#127.0.0.1:5432/test')
Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# create a Session
session = Session()
session._model_changes = {}
return session
#app.route('/company', methods=["GET", "POST"])
def get_all_company():
db = db_connect()
allcompany = db.query(CompanyDemo).join(UsersDemo).all()
return jsonify(company.dump(allcompany, many=True).data) # company is marshmallow schema
if __name__ == '__main__':
app.run(host='0.0.0.0', port=15418, debug=True)
anything wrong in my code? and am facing problem with nested schema and unable to get the nested data in output.
the output below
[ {
"address": "qqq ",
"company_id": 1,
"location": "www ",
"name": "eee" }, {
"address": "www ",
"company_id": 2,
"location": "qqq ",
"name": "aaa" } ]
Self contained example using in-memory SQLite:
from flask import Flask, jsonify
from flask_sqlalchemy import SQLAlchemy
from marshmallow import Schema, fields, pprint
app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'super-secret'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class CompanyDemo(db.Model):
__tablename__ = 'company_demo'
company_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text, nullable=False)
address = db.Column(db.String(50))
location = db.Column(db.String(50))
def __unicode__(self):
return u"{name} ({address})".format(name=self.name, address=self.address)
class UsersDemo(db.Model):
__tablename__ = 'users_demo'
id = db.Column(db.Integer, primary_key=True,)
company_id = db.Column(db.Integer, db.ForeignKey('company_demo.company_id'), nullable=False)
company = db.relationship('CompanyDemo')
email = db.Column(db.String)
def __unicode__(self):
return u"{email}".format(email=self.email)
class CompanySchema(Schema):
company_id = fields.Int(dump_only=True)
name = fields.Str()
address = fields.Str()
location = fields.Str()
class UserSchema(Schema):
email = fields.Str()
company = fields.Nested(CompanySchema)
user_schema = UserSchema()
company_schema = CompanySchema()
#app.route('/')
def index():
return "<a href='/dump_company'>Dump Company</a><br><a href='/dump_user'>Dump User</a>"
#app.route('/dump_user')
def dump_user():
user = UsersDemo.query.first()
return jsonify(user_schema.dump(user).data)
#app.route('/dump_company')
def dump_company():
company = CompanyDemo.query.first()
return jsonify(company_schema.dump(company).data)
def build_db():
db.drop_all()
db.create_all()
company = CompanyDemo(name='Test 1', address='10 Downing Street', location='wherever')
db.session.add(company)
user = UsersDemo(email='fred#example.com', company=company)
db.session.add(user)
db.session.commit()
#app.before_first_request
def first_request():
build_db()
if __name__ == '__main__':
app.run(debug=True, port=7777)