web.py 待办事项列表使用 sqlite 的 int() 无效文字

发布于 2024-11-16 14:39:36 字数 1302 浏览 1 评论 0原文

我正在关注这里的教程 http://webpy.org/docs/0.3/tutorial 然后查看在网上查找如何将待办事项列表部分与 sqlite 一起使用,并找到了这个 http://kzar.co.uk/blog/view/web.py-tutorial-sqlite

我无法通过此错误。我已经搜索过,但我找到的结果都没有对我有太多帮助。大多数人建议将括号中的引号去掉。

错误代码

<type 'exceptions.ValueError'> at /
invalid literal for int() with base 10: '19 02:39:09'

.py

import web

render = web.template.render('templates/')

db = web.database(dbn='sqlite', db='testdb')

urls = (
    '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        todos = db.select('todo')
        return render.index(todos)

if __name__ == "__main__": app.run()

templates/index.html

$def with (todos)
<ul>
$for todo in todos:
    <li id="t$todo.id">$todo.title</li>
</ul>

testbd

CREATE TABLE todo (id integer primary key, title text, created date, done boolean default 'f');
CREATE TRIGGER insert_todo_created after insert on todo
begin
update todo set created = datetime('now')
where rowid = new.rowid;
end;

对于 web.py sqlite 来说非常新

I was following the tutorial here http://webpy.org/docs/0.3/tutorial then looked around the webs to find out how to use the todo list part with sqlite and found this http://kzar.co.uk/blog/view/web.py-tutorial-sqlite

I cannot get passed this error. I have searched and none of the results i can find help me out too much. Most are suggesting to take the quotes out of the parentheses.

Error

<type 'exceptions.ValueError'> at /
invalid literal for int() with base 10: '19 02:39:09'

code.py

import web

render = web.template.render('templates/')

db = web.database(dbn='sqlite', db='testdb')

urls = (
    '/', 'index'
)

app = web.application(urls, globals())

class index:
    def GET(self):
        todos = db.select('todo')
        return render.index(todos)

if __name__ == "__main__": app.run()

templates/index.html

$def with (todos)
<ul>
$for todo in todos:
    <li id="t$todo.id">$todo.title</li>
</ul>

testbd

CREATE TABLE todo (id integer primary key, title text, created date, done boolean default 'f');
CREATE TRIGGER insert_todo_created after insert on todo
begin
update todo set created = datetime('now')
where rowid = new.rowid;
end;

Very new to web.py sqlite

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-11-23 14:39:36

在某个地方,使用参数 '19 02:39:09' 调用 int()int() 无法处理冒号或空格。

>>> int('19 02:39:09')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    int('19 02:39:09')
ValueError: invalid literal for int() with base 10: '19 02:39:09'

>>> int(':')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    int(':')
ValueError: invalid literal for int() with base 10: ':'

>>> int('19 02 39 09')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    int('19 02 39 09')
ValueError: invalid literal for int() with base 10: '19 02 39 09'

>>> int('19023909')
19023909
>>> 

我建议调用 replace() 来删除空格和冒号,如下所示:

>>> date='19 02:39:09'
>>> date=date.replace(" ","")
>>> date
'1902:39:09'
>>> date=date.replace(":","")
>>> date
'19023909'
>>> int(date)  ## It works now!
19023909
>>> 

希望这会有所帮助。

Somewhere, int() is being called with the argument '19 02:39:09'. int() can't handle colons or spaces.

>>> int('19 02:39:09')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    int('19 02:39:09')
ValueError: invalid literal for int() with base 10: '19 02:39:09'

>>> int(':')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    int(':')
ValueError: invalid literal for int() with base 10: ':'

>>> int('19 02 39 09')
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    int('19 02 39 09')
ValueError: invalid literal for int() with base 10: '19 02 39 09'

>>> int('19023909')
19023909
>>> 

I would suggest calling replace() to get rid of the spaces and colons like this:

>>> date='19 02:39:09'
>>> date=date.replace(" ","")
>>> date
'1902:39:09'
>>> date=date.replace(":","")
>>> date
'19023909'
>>> int(date)  ## It works now!
19023909
>>> 

Hope this helps.

哥,最终变帅啦 2024-11-23 14:39:36

只需将“创建”列的类型更改为时间戳:

日期格式为“YYYY-MM-DD”

时间戳-“YYYY-MM-DD HH:MM:SS”

此sql应该可以正常工作:

CREATE TABLE todo (id integer primary key, title text, created timestamp, done boolean default 'f');
CREATE TRIGGER insert_todo_created after insert on todo
begin
update todo set created = datetime('now', 'localtime')
where rowid = new.rowid;
end;

just change type of 'created' column to timestamp:

date format is "YYYY-MM-DD"

timestamp - "YYYY-MM-DD HH:MM:SS"

this sql should work fine:

CREATE TABLE todo (id integer primary key, title text, created timestamp, done boolean default 'f');
CREATE TRIGGER insert_todo_created after insert on todo
begin
update todo set created = datetime('now', 'localtime')
where rowid = new.rowid;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文