【问题标题】:SynaxError on a SQL commandSQL 命令上的 SynaxError
【发布时间】:2020-07-09 18:26:45
【问题描述】:

编辑:是的,我很笨。错别字了。

我正在观看 Udacity 课程中的视频,但在尝试通过 psycopg2 运行 SQL 命令时遇到错误。代码与导师的相同,但我的返回错误而她的没有。

import psycopg2

# establish connection to db
connection = psycopg2.connect('dbname=example')

# cursor is essentially an interface that allows you to start
# cuing up work and transactions
cursor = connection.cursor()

# defines SQL transaction
cursor.execute('''
    CREATE TABLE table2 (
        id INTEGER PRIMARY KEY,
        completed BOOLEAN NOT NULL DEFUALT False
    );
''')

cursor.execute('INSERT INTO table2 (id, completed) VALUES (1, true);')

# commits the transaction
connection.commit()

# must manually close your session each time one is opened
connection.close()
cursor.close()

错误:

$ python3 demo.py
Traceback (most recent call last):
  File "demo.py", line 11, in <module>
    cursor.execute("""
psycopg2.errors.SyntaxError: syntax error at or near "DEFUALT"
LINE 4:         completed BOOLEAN NOT NULL DEFUALT False

【问题讨论】:

  • 你的意思是默认,而不是默认
  • 是的,在询问我何时尝试这样做后,它有一个最短时间,必须等待????
  • 没问题,谢谢! @BrandonZemel

标签: python postgresql psycopg2


【解决方案1】:

您似乎打错字了,而不是您写的DEFAULT DEFUALT

cursor.execute('''
    CREATE TABLE table2 (
        id INTEGER PRIMARY KEY,
        completed BOOLEAN NOT NULL DEFAULT False
    );
''')

【讨论】:

    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 2012-05-31
    • 2013-08-28
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多