[英]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.