sqlite objects in thread can only be used in the same thread - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: sqlite objects in thread can only be used in the same thread (/thread-21985.html) |
sqlite objects in thread can only be used in the same thread - darktitan - Oct-23-2019 hi. I made a table showing user accounts in my app and i tought it would be a good thing to add an edit function to it so i can edit the accounts. Every thing works till i get to the saveing part of the edit function there i get this error Something like that. The funny thing i get this error on my laptop but not on my stationary but also nothing happens there i can spam the save button and nothing happens all i see in the console is POST msg.Could any one help me with this? I can provide more code if this i post is not enough. main.py This is for the table. @app.route('/show_users') @login_required(role="admin") def show_users(): results = [] qry = db_session.query(User) results = qry.all() if not results: flash('Inget kunde hittas!') return redirect('adminindex') else: # display results table = Users(results) table.border = True return render_template('show_users.html', table=table)this is for the account edit function. @app.route('/redigera_anvandare/<int:id>', methods=['GET', 'POST']) @login_required(role="admin") def redigera_anvandare(id): """ Add / edit an item in the database """ qry = db_session.query(User).filter( User.id==id) anvandare = qry.first() if anvandare: form = UserForm(formdata=request.form, obj=anvandare) if request.method == 'POST' and form.validate(): # save edits save_changes3(anvandare, form) flash('Artikeln har blivit uppdaterad!') return redirect('/adminindex') return render_template('redigera_användare.html', form=form) else: return 'Error loading #{id}'.format(id=id)Forms.py class UserForm(FlaskForm): user = StringField('Användarnamn:', validators=[DataRequired()]) password = StringField('Lösenord:', validators=[DataRequired()]) #owner = StringField('Konto ägare:', validators=[DataRequired()]) urole = RadioField('Användar Behörighet:', choices=[('admin', 'administratör'),('user', 'Användare')]) submit = SubmitField("Sign In")models.py class User(db.Model): __tablename__ = "anvandare" id = db.Column(db.Integer, primary_key=True) user = db.Column(db.String(80), unique=True) password = db.Column(db.String(80)) #owner = db.Column(db.String(80)) urole = db.Column(db.String(80)) def __init__(self, user, password, urole): self.user = user self.password = password #self.owner = owner self.urole = urole def __repr__(self): return '<User %r>' % self.user def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return str(self.user)db_setup.py from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = create_engine('sqlite:///Matrialdb.db', connect_args={'check_same_thread': False}, convert_unicode=True) db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base = declarative_base() Base.query = db_session.query_property() def init_db(): import models Base.metadata.create_all(bind=engine) RE: sqlite objects in thread can only be used in the same thread - Larz60+ - Oct-23-2019 This looks like a pretty good article on sharing data between threads, covers several methods: https://www.pythonforthelab.com/blog/handling-and-sharing-data-between-threads/ RE: sqlite objects in thread can only be used in the same thread - snippsat - Oct-23-2019 When you use Flask should use Flask-SQLAlchemy and not SQLAlchemy directly. Quote:Flask-SQLAlchemy aims to simplify using SQLAlchemy with Flask, RE: sqlite objects in thread can only be used in the same thread - darktitan - Oct-24-2019 (Oct-23-2019, 07:22 PM)Larz60+ Wrote: This looks like a pretty good article on sharing data between threads, covers several methods: https://www.pythonforthelab.com/blog/handling-and-sharing-data-between-threads/ But im not using threads. (Oct-23-2019, 07:37 PM)snippsat Wrote: When you use Flask should use Flask-SQLAlchemy and not SQLAlchemy directly. I am following this guys tutorial on the matter of the database https://www.blog.pythonlibrary.org/2017/12/12/flask-101-adding-a-database/ My app.py looks like this from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///Matrialdb.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.secret_key = "flask rocks!" app.debug = True db = SQLAlchemy(app) RE: sqlite objects in thread can only be used in the same thread - snippsat - Oct-24-2019 (Oct-24-2019, 03:51 AM)darktitan Wrote: My app.py looks like thisThen you don't need db_setup.py at all,it's maybe confusing because he mixing this stuff together in tutorial.He do mention this. Mike Driscoll Wrote:You will note that Flask-SQLAlchemy doesn’t require all the imports that just plain SQLAlchemy required. RE: sqlite objects in thread can only be used in the same thread - darktitan - Oct-24-2019 (Oct-24-2019, 05:08 AM)snippsat Wrote:(Oct-24-2019, 03:51 AM)darktitan Wrote: My app.py looks like thisThen you don't need Nice to know. I did the modification needed and removed SQLAlchemy but im still got same problem as before. I cant save the changes i do to the account. and im not getting any error on my stationary computer. In console i can see the POST but nothing more. And when i enter the db file nothing have changed. Funny thing is i use almost the identical code in another thing in an another table in the db and it works the changes gets saved and i get redirected to another page. eureka I hade put FlaskForm instead of Form In my Form for the page class UserForm(Form): user = StringField('Användarnamn:', validators=[DataRequired()]) password = StringField('Lösenord:', validators=[DataRequired()]) urole = StringField('Användar Behörighet:', validators=[DataRequired()]) owner = StringField('Användar namn:', validators=[DataRequired()])Got it to work now. |