web.py todo list使用sqlite无效的文字用于int()

时间:2011-06-19 02:46:07

标签: python sqlite web.py

我在这里按照教程http://webpy.org/docs/0.3/tutorial然后浏览网页以了解如何使用sqlite使用todo列表部分并找到此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'

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()

模板/ 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

2 个答案:

答案 0 :(得分:3)

在某处,使用参数int()调用'19 02:39:09'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
>>> 

希望这有帮助。

答案 1 :(得分:1)

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

日期格式为“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;