This is my Bottle code
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)
I am trying to make the make the edit_item method to work. When I call it with curl curl -X PUT -d "name=aa&category=bb&amount=23&location=xx&date=21-10-2014" http://localhost:8080/inventory/edit/2
I get a server error which says
raise AttributeError('Atrribute %r is not defined.' % name) AttributeError: Attribute 'PUT' not defined' What should i do ?
Instead of this,
name = request.PUT.get('name')
use this:
name = request.params.get('name')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.