在this Flask tutorial之后,我一直收到以下错误:
File "db.py", line 29, in init_db_command
init_db()
个字符
sqlite3.OperationalError:“作者ID”附近:语法错误
schema.sql:
DROP TABLE IF EXISTS user_tb;
DROP TABLE IF EXISTS post;
CREATE TABLE user_tb (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE post (
id INTEGER PRIMARY KEY AUTOINCREMENT,
author id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY author_id REFERENCES user_tb id
);
型
网址:db.py
import sqlite3
import click
from flask import current_app, g
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def init_db():
db = get_db()
with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
@click.command('init-db')
def init_db_command():
init_db()
click.echo('Initialized the database.')
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)
型
第一个月
import os
from flask import Flask
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SECRET_KEY='dev',
DATABASE=os.path.join(app.instance_path, 'flaskr.sqlite'),
)
if test_config is None:
app.config.from_pyfile('config.py', silent=True)
else:
app.config.from_mapping(test_config)
try:
os.makedirs(app.instance_path)
except OSError:
pass
@app.route('/hello')
def hello():
return 'Hello World!'
from . import db
db.init_app(app)
from . import auth
app.register_blueprint(auth.bp)
return app
型
我将user_tb
表从user
更改为user_tb
,但没有任何更改。我更改了外键名,但没有更改。我在author_id
和user_tb
id两边使用了括号。它仅从以下内容更改:
sqlite3.OperationalError:“作者ID”附近:语法错误
收件人:
sqlite3.OperationalError:near(:语法错误
最初的错误是:
sqlite3.OperationalError:近时间戳:语法错误
已通过将CURRENT TIMESTAMP
更改为CURRENT_TIMESTAMP
修复了该问题。
1条答案
按热度按时间xv8emn3q1#
感谢Michael Butscher的评论。看起来我的author_id整数没有下划线,而外键author_id有。另外,当引用user_tb的id时,应该只有id在括号中。下面是post表的更新代码块,其中星号中有更改:
字符串