使用SQLITE3的Pythons Bottle

时间:2017-02-08 11:00:50

标签: python sqlite bottle

我一直在努力练习Bottle Py。有一个关于制作APP的教程:TODO。

工作正常。但如果任务ID超过1个字符,则意味着10而不是1,2,3,4,5,6,7,8,9

它显示如下错误。

  

ProgrammingError('提供的绑定数量不正确。当前   声明使用1,并且提供了2个。',)

enter image description here

代码是:

@route('/edit/<no:int>', method='GET')
def edit_item(no):

    if request.GET.save:
        edit = request.GET.task.strip()
        status = request.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 template('edit_task', old=cur_data, no=no)

回溯:

1

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/usr/lib/python2.7/dist-packages/bottle.py", line 1737, in wrapper
    rv = callback(*a, **ka)
  File "todo.py", line 67, in edit_item
    c.execute('SELECT task FROM todo WHERE id LIKE ?', no)
ValueError: parameters are of unsupported type

2

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/bottle.py", line 862, in _handle
    return route.call(**args)
  File "/usr/lib/python2.7/dist-packages/bottle.py", line 1737, in wrapper
    rv = callback(*a, **ka)
  File "todo.py", line 67, in edit_item
    c.execute('SELECT task FROM todo WHERE id LIKE ?', (no))
ValueError: parameters are of unsupported type

怎么办?

1 个答案:

答案 0 :(得分:2)

可能会发生这种情况,因为execute函数会在(str(no)) ()外部(str(no),)无法转换元组时解压缩第二个参数,如果"10",则需要执行("1", "0")你在元组中只有一个元素。

例如,由于它被识别为字符串,因此会将var fs = require('fs') var logger = fs.createWriteStream('log.txt', { flags: 'a' // 'a' means appending (old data will be preserved) }) logger.write('some data') // append string to your file logger.write('more data') // again logger.write('and more') // again 解包为url(r'^(?P<my_var>:|var1|var2|var3|other_string)/rest_of_url'