简体   繁体   English

尝试在 sqlalchemy => sqlalchemy.exc.OperationalError 中创建我的表时出错:(sqlite3.OperationalError) 没有这样的表:dht_new

[英]Error when trying to create my table in sqlalchemy => sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: dht_new

It cant find my db even tho i have created it wite create_table?即使我已经用 create_table 创建了它,它也找不到我的数据库? I have tried to put the function create_Table" in diffrent places with no result? However i get the error,我试图将函数 create_Table" 放在不同的地方但没有结果?但是我得到了错误,

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: dht_new
[SQL: INSERT INTO dht_new (name) VALUES (?)]
[parameters: (None,)]

app.py应用程序

import os
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from flask_sqlalchemy import SQLAlchemy
from db import db


app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI', 'sqlite:///newdata.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

@app.before_first_request
def create_tables():
    db.create_all()


class fixdb(db.Model):
    __tablename__ = 'dht_new'

    id = db.Column(db.Integer, primary_key =True)
    name = db.Column(db.String(80))


    def __init__(self, temp):
        self.temp = temp

    def add_to_db(self):
        db.session.add(self)
        db.session.commit()


@app.route('/get', methods=['GET'])
def postrandom():
    temp = {'temperature': '23'}
    test_temp = fixdb(temp)

    test_temp.add_to_db()
    return jsonify(temp)



if __name__ == '__main__':
    from db import db
    db.init_app(app)
    app.run(port=5000, debug=True)

run.py运行文件

from app import app
from db import db

db.init_app(app)

@app.before_first_request
def create_tables():
    db.create_all()

You need to create table first in your SQLite database.您需要先在 SQLite 数据库中创建表。 You can use the SQLite Browser to do the same else use the python CLI to create the table.您可以使用SQLite Browser执行相同的操作,否则使用 python CLI 创建表。 Since you didn't mention your .py file name so I can't exactly show you the code but it would be something like this由于你没有提到你的.py文件名,所以我不能准确地向你展示代码,但它会是这样的
first type python in your terminal, then首先在终端中输入python ,然后

from app import db
db.drop_all()
db.create_all()

and this should do it for you.这应该为你做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:用户 - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:项目 - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: items SQLAlchemy.exc.OperationalError : (sqlite3.OperationalError) 没有这样的表:用户 - SQLAlchemy.exc.OperationalError : (sqlite3.OperationalError) no such table: user sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:mytable - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: mytable sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表: - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: flask sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:用户 - flask sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: users 运行 Airflow 时如何解决错误“sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: session” - How to solve error "sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: session" when running Airflow sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) 没有这样的表:用户。 无法为用户创建表格 - sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user. Can't make a table for user flask db init 返回“sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表” - flask db init returns "sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table" cursor.execute(语句,参数)sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有此类表 - cursor.execute(statement, parameters) sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM