Bottle实例Todo-List—编辑数据库中的记录

代码如下:

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
@bottle.route( '/edit/:no', method= 'GET')
def edit_item(no):

     if bottle.request.GET. get( 'save', ''). strip():
        edit = bottle.request.GET. get( 'task', ''). strip()
        status = bottle.request.GET. get( 'status', ''). strip()

         if status ==  'open':
            status =  1
         else:
            status =  0

        conn = sqlite3.connect( 'todo.db')
        c = conn.cursor()
        c.execute( "UPDATE todo SET task = ?, status = ? WHERE id LIKE ?", (edit, status, no))
        conn.commit()

         return  '<p>The item number %s was successfully updated</p>' % no
     else:
        conn = sqlite3.connect( 'todo.db')
        c = conn.cursor()
        c.execute( "SELECT task FROM todo WHERE id LIKE ?", ( str(no)))
        cur_data = c.fetchone()

         return bottle.template( 'edit_task', old=cur_data, no=no)

bottle.debug( True)
bottle.run(host= '127.0.0.1', port= 8080, reloader =  True)

edit_task.tpl模板代码如下:

template code
1
2
3
4
5
6
7
8
9
10
11
12
% #template for editing a task
% #the template expects to receive a value for "no" as well a "old", the text of the selected ToDo item
<p>Edit the task  with ID = {{no}}</p>
<form action= "/edit/{{no}}" method= "get">
< input  type= "text" name= "task" value= "{{old[0]}}" size= "100" maxlength= "100">
<select name= "status">
<option> open</option>
<option>closed</option>
</select>
<br/>
< input  type= "submit" name= "save" value= "save">
</form>

同样上面代码用到todo.db数据表,在浏览器中输入:http://127.0.0.1:8080/edit/4

得到如下:


HTML表中的open 和closeed选项分别控制数据标准记录项的"status",的值 0或1: 选open ,点击save,得到如下结果:


同样的方法根据ID(目前有:1、2、3、4、5、6)不同操作各记录项。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值