web.py todo list using sqlite invalid literal for int()
I was following the tutor开发者_开发知识库ial 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
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.
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;
精彩评论