簡體   English   中英

表名稱的SQL語法錯誤

[英]SQL syntax error on table name

我剛剛結束了有關在論壇數據庫中阻止垃圾郵件的內容,現在是時候清理它們了。

目標是使用update語句來更新數據庫記錄並刪除標記為垃圾郵件的帖子。 撓頭並從在線講師(這是在線課程)上收到含糊的提示和指示后,我遇到了一個錯誤。

這可能不是執行此操作的正確方法,並且在Google運氣不好之后,我正在尋找指導。 這是forumdb.py的片段:

# "Database code" for the DB Forum.

import bleach
import psycopg2
import datetime

DBNAME = "forum"

def get_posts():
  """Return all posts from the 'database', most recent first."""
  db = psycopg2.connect(database=DBNAME)
  c = db.cursor()
  UPDATE posts
    SET content = 'cheese'
    WHERE content like 'spam' ;
  c.execute("select content, time from posts order by time desc")
  return c.fetchall()
  db.close()

def add_post(content):
  """Add a post to the 'database' with the current timestamp."""
  db = psycopg2.connect(database=DBNAME)
  c = db.cursor()
  clean_cont = bleach.clean(content)
  c.execute("insert into posts values (%s)", (clean_cont,))
  db.commit()
  db.close()

這是我得到的錯誤:

Traceback (most recent call last):
  File "forum.py", line 7, in <module>
    from forumdb import get_posts, add_post
  File "/vagrant/forum/forumdb.py", line 13
    UPDATE posts
           ^
SyntaxError: invalid syntax

如果有幫助,請訪問forum.py:

#!/usr/bin/env python3
# 
# A buggy web service in need of a database.

from flask import Flask, request, redirect, url_for

from forumdb import get_posts, add_post

app = Flask(__name__)

# HTML template for the forum page
HTML_WRAP = '''\
<!DOCTYPE html>
<html>
  <head>
    <title>DB Forum</title>
    <style>
      h1, form { text-align: center; }
      textarea { width: 400px; height: 100px; }
      div.post { border: 1px solid #999;
                 padding: 10px 10px;
                 margin: 10px 20%%; }
      hr.postbound { width: 50%%; }
      em.date { color: #999 }
    </style>
  </head>
  <body>
    <h1>DB Forum</h1>
    <form method=post>
      <div><textarea id="content" name="content"></textarea></div>
      <div><button id="go" type="submit">Post message</button></div>
    </form>
    <!-- post content will go here -->
%s
  </body>
</html>
'''

# HTML template for an individual comment
POST = '''\
    <div class=post><em class=date>%s</em><br>%s</div>
'''


@app.route('/', methods=['GET'])
def main():
  '''Main page of the forum.'''
  posts = "".join(POST % (date, text) for text, date in get_posts())
  html = HTML_WRAP % posts
  return html


@app.route('/', methods=['POST'])
def post():
  '''New post submission.'''
  message = request.form['content']
  add_post(message)
  return redirect(url_for('main'))


if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8000)

在此先感謝任何有才干的人來幫助您!

您的查詢

UPDATE posts
SET content = 'cheese'
WHERE content like 'spam' ;

超出范圍。 將其刪除或放在c.execute()函數上,如下所示

c.execute("UPDATE posts SET content = 'cheese' WHERE content like 'spam';");

這就是為什么您會收到此錯誤,希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM