cari

Rumah  >  Soal Jawab  >  teks badan

python - Pemahaman mendalam tentang Flask Bab 3: Ralat dilaporkan semasa menjalankan kod Kod ini benar-benar normal Mengapa ralat sintaks SQL dilaporkan?

1

2

<code>原句

posts = Post.query.order_by(Post.publish_date.desc()).paginate(page, 10)</code>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<code>路由这里已经定义 page=1了, 作者不可能写错代码啊, 我设置了utf-8编码也不行

@app.route('/')

@app.route('/<int:page>')

def home(page=1):

    posts = Post.query.order_by(Post.publish_date.desc()).paginate(page, 10)

    recent, top_tags = sidebar_data()

 

    return render_template(

        'home.html',

        posts=posts,

        recent=recent,

        top_tags=top_tags

    )

</code>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

<code>/Library/Python/2.7/site-packages/sqlalchemy/sql/compiler.py:624: SAWarning: Can't resolve label reference 'total DESC'; converting to text() (this warning may be suppressed after 10 occurrences)

  util.ellipses_string(element.element))

127.0.0.1 - - [07/Jun/2017 14:44:06] "GET / HTTP/1.1" 500 -

Traceback (most recent call last):

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1994, in __call__

    return self.wsgi_app(environ, start_response)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1985, in wsgi_app

    response = self.handle_exception(e)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1540, in handle_exception

    reraise(exc_type, exc_value, tb)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1982, in wsgi_app

    response = self.full_dispatch_request()

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request

    rv = self.handle_user_exception(e)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1517, in handle_user_exception

    reraise(exc_type, exc_value, tb)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request

    rv = self.dispatch_request()

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1598, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/main.py", line 114, in home

    top_tags=top_tags

  File "/Library/Python/2.7/site-packages/flask/templating.py", line 134, in render_template

    context, ctx.app)

  File "/Library/Python/2.7/site-packages/flask/templating.py", line 116, in _render

    rv = template.render(context)

  File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 1008, in render

    return self.environment.handle_exception(exc_info, True)

  File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 780, in handle_exception

    reraise(exc_type, exc_value, tb)

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/home.html", line 1, in top-level template code

    {% extends "base.html" %}

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/base.html", line 45, in top-level template code

    {% block body %}

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/home.html", line 38, in block "body"

    {{ render_pagination(posts, 'home') }}

  File "/Library/Python/2.7/site-packages/jinja2/runtime.py", line 551, in _invoke

    rv = self._func(*arguments)

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/base.html", line 5, in template

    <a href="{{ url_for('home', page=pagination.prev().page) }}" aria-label="Previous">

  File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 338, in prev

    return self.query.paginate(self.page - 1, self.per_page, error_out)

  File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 475, in paginate

    items = self.limit(per_page).offset((page - 1) * per_page).all()

  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/query.py", line 2703, in all

    return list(self)

  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/query.py", line 2855, in __iter__

    return self._execute_and_instances(context)

  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/query.py", line 2878, in _execute_and_instances

    result = conn.execute(querycontext.statement, self._params)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute

    return meth(self, multiparams, params)

  File "/Library/Python/2.7/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection

    return connection._execute_clauseelement(self, multiparams, params)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement

    compiled_sql, distilled_params

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context

    context)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception

    exc_info

  File "/Library/Python/2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause

    reraise(type(exception), exception, tb=exc_tb, cause=cause)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context

    context)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute

    cursor.execute(statement, parameters)

  File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute

    self.errorhandler(self, exc, value)

  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler

    raise errorclass, errorvalue

ProgrammingError: (_mysql_exceptions.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-10, 10' at line 3") [SQL: u'SELECT post.id AS post_id, post.title AS post_title, post.text AS post_text, post.publish_date AS post_publish_date, post.user_id AS post_user_id \nFROM post ORDER BY post.publish_date DESC \n LIMIT %s, %s'] [parameters: (-10, 10)]

127.0.0.1 - - [07/Jun/2017 14:44:06] "GET /?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -

127.0.0.1 - - [07/Jun/2017 14:44:06] "GET /?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -

127.0.0.1 - - [07/Jun/2017 14:44:06] "GET /?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -

(<type 'int'>, 1)

127.0.0.1 - - [07/Jun/2017 14:44:06] "GET / HTTP/1.1" 500 -

Traceback (most recent call last):

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1994, in __call__

    return self.wsgi_app(environ, start_response)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1985, in wsgi_app

    response = self.handle_exception(e)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1540, in handle_exception

    reraise(exc_type, exc_value, tb)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1982, in wsgi_app

    response = self.full_dispatch_request()

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request

    rv = self.handle_user_exception(e)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1517, in handle_user_exception

    reraise(exc_type, exc_value, tb)

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request

    rv = self.dispatch_request()

  File "/Library/Python/2.7/site-packages/flask/app.py", line 1598, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/main.py", line 114, in home

    top_tags=top_tags

  File "/Library/Python/2.7/site-packages/flask/templating.py", line 134, in render_template

    context, ctx.app)

  File "/Library/Python/2.7/site-packages/flask/templating.py", line 116, in _render

    rv = template.render(context)

  File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 1008, in render

    return self.environment.handle_exception(exc_info, True)

  File "/Library/Python/2.7/site-packages/jinja2/environment.py", line 780, in handle_exception

    reraise(exc_type, exc_value, tb)

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/home.html", line 1, in top-level template code

    {% extends "base.html" %}

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/base.html", line 45, in top-level template code

    {% block body %}

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/home.html", line 38, in block "body"

    {{ render_pagination(posts, 'home') }}

  File "/Library/Python/2.7/site-packages/jinja2/runtime.py", line 551, in _invoke

    rv = self._func(*arguments)

  File "/Users/andy/Desktop/Mastering Flask_Code Bundle/Chapter 3/chapter_3/templates/base.html", line 5, in template

    <a href="{{ url_for('home', page=pagination.prev().page) }}" aria-label="Previous">

  File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 338, in prev

    return self.query.paginate(self.page - 1, self.per_page, error_out)

  File "/Library/Python/2.7/site-packages/flask_sqlalchemy/__init__.py", line 475, in paginate

    items = self.limit(per_page).offset((page - 1) * per_page).all()

  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/query.py", line 2703, in all

    return list(self)

  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/query.py", line 2855, in __iter__

    return self._execute_and_instances(context)

  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/query.py", line 2878, in _execute_and_instances

    result = conn.execute(querycontext.statement, self._params)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 945, in execute

    return meth(self, multiparams, params)

  File "/Library/Python/2.7/site-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection

    return connection._execute_clauseelement(self, multiparams, params)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement

    compiled_sql, distilled_params

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context

    context)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception

    exc_info

  File "/Library/Python/2.7/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause

    reraise(type(exception), exception, tb=exc_tb, cause=cause)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context

    context)

  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute

    cursor.execute(statement, parameters)

  File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute

    self.errorhandler(self, exc, value)

  File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler

    raise errorclass, errorvalue

ProgrammingError: (_mysql_exceptions.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-10, 10' at line 3") [SQL: u'SELECT post.id AS post_id, post.title AS post_title, post.text AS post_text, post.publish_date AS post_publish_date, post.user_id AS post_user_id \nFROM post ORDER BY post.publish_date DESC \n LIMIT %s, %s'] [parameters: (-10, 10)]</code>

巴扎黑巴扎黑2839 hari yang lalu1024

membalas semua(2)saya akan balas

  • 巴扎黑

    巴扎黑2017-06-12 09:25:38

    Lihat pernyataan sql anda

    1

    <code>select * from 表 limit -10, 10</code>

    Yang betul sepatutnya

    1

    2

    <code>select * from 表 limit 0, 10

    </code>

    Input ke halaman anda ialah 0, bukan? Sekurang-kurangnya ia bermula dengan 1

    balas
    0
  • 漂亮男人

    漂亮男人2017-06-12 09:25:38

    Pangkalan data, jadual belum dibina lagi

    balas
    0
  • Batalbalas