通过指向my last question,我正在尝试将数据插入数据库。尝试这个:
db.session.add(slidephoto(uid='2016', photo='niloofar.jpg'))
此错误提升:
AttributeError: 'function' object has no attribute 'session'
如果需要我的导入和配置:
import os, sys
from niloofar import *
from flask import Flask, request, url_for, render_template, make_response, redirect
from flask_sqlalchemy import SQLAlchemy
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://myusername:mypassword@localhost/mydbname'
db = SQLAlchemy(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
这是我的数据库模型:
class slidephoto(db.Model):
__tablename__ = 'slide_photo'
id = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.Integer, nullable=False)
photo = db.Column(db.String(collation='utf8_bin'), nullable=False)
def __init__(self, uid, photo):
self.uid = uid
self.photo = photo
def __repr__(self):
return "{'photo': " + str(self.photo) + "}"
============== update ===============
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine('mysql://myusername:mypassword@localhost/mydbname')
db_session = scoped_session(sessionmaker(bind=engine))