Bottle实例Todo-List—在数据库中插入一条记录
来源:互联网 发布:外卖消费人群数据分析 编辑:程序博客网 时间:2024/05/29 14:38
代码如下:
Python Code
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
# -*- coding: utf-8 -*-
#!/usr/bin/python
# filename:insetList.py
# codedtime: 2014-8-26 22:52:29
import sqlite3
import bottle
@bottle.route('/new', method='GET')
def new_item():
new = bottle.request.GET.get('task', '').strip()
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,1))
new_id = c.lastrowid
conn.commit()
c.close()
return '<p>The new task was inserted into the database, the ID is %s</p>' % new_id
bottle.debug(True)
bottle.run(host='127.0.0.1', port=8080, reloader = True)
#!/usr/bin/python
# filename:insetList.py
# codedtime: 2014-8-26 22:52:29
import sqlite3
import bottle
@bottle.route('/new', method='GET')
def new_item():
new = bottle.request.GET.get('task', '').strip()
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,1))
new_id = c.lastrowid
conn.commit()
c.close()
return '<p>The new task was inserted into the database, the ID is %s</p>' % new_id
bottle.debug(True)
bottle.run(host='127.0.0.1', port=8080, reloader = True)
输出结果:
说明插入了一条记录,其ID为5。
下面是使用模板(template)对本程序的扩展,代码如下:
Python Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@bottle.route('/new2', method='GET')
def new_item():
if bottle.request.GET.get('save','').strip():
new = bottle.request.GET.get('task', '').strip()
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,0))
new_id = c.lastrowid
conn.commit()
c.close()
return '<p>The new task was inserted into the database, the ID is %s</p>' % new_id
else:
return bottle.template('new_task.tpl')
def new_item():
if bottle.request.GET.get('save','').strip():
new = bottle.request.GET.get('task', '').strip()
conn = sqlite3.connect('todo.db')
c = conn.cursor()
c.execute("INSERT INTO todo (task,status) VALUES (?,?)", (new,0))
new_id = c.lastrowid
conn.commit()
c.close()
return '<p>The new task was inserted into the database, the ID is %s</p>' % new_id
else:
return bottle.template('new_task.tpl')
2
3
4
5
<p>Add a new task to the ToDo list:</p>
<form action="/new2" method="GET">
<input type="text" size="100" maxlength="100" name="task">
<input type="submit" name="save" value="save">
</form>
<form action="/new2" method="GET">
<input type="text" size="100" maxlength="100" name="task">
<input type="submit" name="save" value="save">
</form>
在浏览器中输入:http://127.0.0.1:8080/new2
得到如下结果:
输入内容如:Hello,Johnny,how are you!!! 点击:save 则插入一条记录,其浏览器会显示:
表明插入数据成功。
0 0
- Bottle实例Todo-List—在数据库中插入一条记录
- Bottle实例Todo-List—编辑数据库中的记录
- Bottle实例Todo-List—用SQLite3创建数据库
- Bottle实例Todo-List—查询并显示数据库中的内容
- Bottle实例Todo-List—动态路由中使用正则表达式
- 利用PreparedStatement对象实现在数据库中插入一条记录。
- TODO:从数据库中随机抽取一条记录
- Bottle实例Todo-List—返回静态文件和json对象
- VC向数据库中插入一条记录
- 数据库中插入一条记录后获取主键id
- android android 在list view中插入一条广告
- SQLITE中插入一条记录
- 在gridControl中删除一条数据库记录(C#版)
- 在gridControl中删除一条数据库记录(C#版)
- 在DNS数据库中插入记录
- 如何往MSSQLServer中插入一条记录
- 在access数据库中,据记录ID得到其上一条记录及下一条记录的SQL
- 用一条SQL语句实现向数据库中插入多条记录的方法
- STM32系列芯片序列号的获取
- Dancing link 与精确匹配问题
- dtc 命令
- hdu1561--H - ACboy needs your help(树形dp)
- c++实现链表的基本操作
- Bottle实例Todo-List—在数据库中插入一条记录
- c中常用string.h函数介绍
- Android网络连接判断与处理
- 嘛~付钱吧!(宁波工程学院)
- Android 高仿微信实时聊天 基于百度云推送
- ListView的例子
- Android应用开发:Dialog使用123
- [Sqlite]-->嵌入式数据库的安装、建库、建表、更新表结构以及数据导入导出等等详细过程记录
- 解决关灯游戏(Lights Off)