【问题标题】:sqlite3.OperationalError: near "create": syntax errorsqlite3.OperationalError:“创建”附近:语法错误
【发布时间】:2020-02-20 15:04:51
【问题描述】:

我的schema.sql 文件中出现了一个相当奇怪的语法错误。

错误如下:

文件“/Users/user/Documents/GitProjects/FlaskApp/flaskr/db.py”,行 88,在 init_db_command init_db() 文件“/Users/user/Documents/GitProjects/FlaskApp/flaskr/db.py”,第 80 行,在 初始化数据库 db.executescript(f.read().decode("utf-8")) sqlite3.OperationalError: near "create": 语法错误

我的架构如下所示:

drop table if exists post;
drop table if exists post_user;

create table post_user (
    user_id integer primary key autoincrement,
    email text unique not null,
    username text unique not null,
    password text not null
);

create table post (
    post_id integer primary key autoincrement,
    author_id integer not null,
    create timestamp not null default current_timestamp,
    title text not null,
    body text not null,
    foreign key (author_id) references post_user (user_id)
);

我的 python 看起来像这样:

def init_db():
    db = get_db()
    with current_app.open_resource("schema.sql") as f:
        db.executescript(f.read().decode("utf-8"))

最后是目录:

root
 |--db.py
 |--schema.sql
instance
 |-- project.sqlite

我正在使用烧瓶和 sqlite3。 我的故障排除步骤如下:

1) 删除除第一行以外的所有内容(可行)

2) 删除了除前两行之外的所有内容(这没有)

3) 删除./instance/project.sqlite 文件

4) 使用在线验证器 这特别有趣,因为这发现每个单独的命令都有效。 但是当我将两个背靠背串起来时,它说命令无效。

5) 仔细检查有关删除表的 sqlite 文档

6) 甚至确保我的.vimrc 设置为utf-8

我确定我在这里遗漏了一些小东西,但似乎没有任何效果。 任何帮助将不胜感激!

【问题讨论】:

  • 我做到了?它在那里的第二个街区。这是导致错误的原因。这就是我的 python 调用的内容

标签: python sqlite


【解决方案1】:

您可能希望 create timestamp 列类似于 create_timestamp 以避免与 sql 关键字冲突。

【讨论】:

  • 这就是问题所在。我偶然将created TIMESTAMP... 更改为create timestamp。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
相关资源
最近更新 更多