这是我的代码
import sqlite3
import json
from bottle import route, run, request
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def db_connect():
conn = sqlite3.connect('inventory.db')
conn.row_factory = dict_factory
return conn, conn.cursor()
@route('/inventory', method='GET')
def get_inventory():
conn,c=db_connect()
c.execute("SELECT id, name, category, location, date, amount FROM inventory")
result = c.fetchall()
json_result=json.dumps(result)
return json_result
@route('/inventory/get/:id', method='GET')
def get_item(id):
conn,c=db_connect()
c.execute("SELECT id, name, category, location, date, amount FROM inventory WHERE id=?",(id, ))
result=c.fetchall()
json_result=json.dumps(result)
return json_result
@route('/inventory/new', method='POST')
def add_item():
name = request.POST.get('name')
category = request.POST.get('category')
location = request.POST.get('location')
date = request.POST.get('date')
amount = request.POST.get('amount')
conn,c=db_connect()
c.execute("INSERT INTO inventory (name, category, location, date, amount) VALUES (?,?,?,?,?)", (name,category,location,date,amount))
new_id = c.lastrowid
conn.commit()
c.close()
return '<p>The entry with id %d has been added to the database</p>' %new_id
@route('/inventory/delete/:id', method='DELETE')
def delete_item(id):
conn,c=db_connect()
c.execute("DELETE FROM inventory WHERE id =?", (id, ))
conn.commit()
c.close()
return 'The entry with id %s has been deleted from the database' %id
@route('/inventory/edit/:id', method='PUT')
def edit_item(id):
name = request.PUT.get('name')
category = request.PUT.get('category')
amount = request.PUT.get('location')
location = request.PUT.get('date')
date = request.PUT.get('amount')
conn,c=db_connect()
c.execute("UPDATE Inventory SET name=?, category=?, location=?, date=?, amount=? WHERE id=?", (name, category, location, date, amount,id))
conn.commit()
c.close();
return '<p>The entry with id %s has been edited in the database</p>' %id
run(reloader=True)
我正在尝试使edit_item方法起作用。
当我用curl调用它时
curl -X PUT -d "name=aa&category=bb&amount=23&location=xx&date=21-10-2014" http://localhost:8080/inventory/edit/2
我收到服务器错误,上面写着
引发AttributeError('Atrribute%r未定义。'%name) AttributeError:属性'PUT'未定义' 我该怎么办?
答案 0 :(得分:0)
而不是这个,
name = request.PUT.get('name')
使用它:
name = request.params.get('name')