【问题标题】:python error binding parameter 0 - probably unsupported typepython错误绑定参数0 - 可能不支持的类型
【发布时间】:2014-06-14 19:09:33
【问题描述】:

我正在使用 XBMC 媒体应用程序的 2.6 版编写我的 python 脚本。

我的 python 脚本有问题,我收到一个错误:错误内容:错误绑定参数 0 - 可能是不受支持的类型。

错误在这一行跳转:

cur.execute('SELECT * FROM programs WHERE channel=? AND start_date <= ? AND stop_date >= ?', [channel, now, now])

代码如下:

import xbmc
import xbmcgui
import xbmcaddon
import os
import urllib2
import StringIO
import sqlite3
from sqlite3 import dbapi2 as database
from xml.etree import ElementTree
import xml.etree.ElementTree as ET
from UserDict import DictMixin
import datetime
import time

class MyClass(xbmcgui.WindowXML):

    def onAction(self, action):

        #DOWNLOAD THE XML SOURCE HERE
        url = ADDON.getSetting('allchannels.url')
        req = urllib2.Request(url)
        response = urllib2.urlopen(req)
        data = response.read()
        response.close()
        profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))

        if os.path.exists(profilePath):
           profilePath = profilePath + 'source.db'
           con = database.connect(profilePath)
           cur = con.cursor()
           cur.execute('CREATE TABLE programs(channel TEXT, title TEXT, start_date TIMESTAMP, stop_date TIMESTAMP, description TEXT)')
           con.commit()
           con.close
           tv_elem = ElementTree.parse(StringIO.StringIO(data)).getroot()
           profilePath = xbmc.translatePath(os.path.join('special://userdata/addon_data/script.tvguide', ''))
           profilePath = profilePath + 'source.db'
           con = sqlite3.connect(profilePath)
           cur = con.cursor()
           channels = OrderedDict()

           # Get the loaded data
           for channel in tv_elem.findall('channel'):
           channel_name = channel.find('display-name').text
           for program in channel.findall('programme'):
               title = program.find('title').text
               start_time = program.get("start")
               stop_time = program.get("stop")
               cur.execute("INSERT INTO programs(channel, title, start_date, stop_date)" + " VALUES(?, ?, ?, ?)", [channel_name, title, start_time, stop_time])
               con.commit()
               print 'Channels store into database are now successfully!'


           program = None
           now = datetime.datetime.now()
           #strCh = '(\'' + '\',\''.join(channelMap.keys()) + '\')'
           cur.execute('SELECT * FROM programs WHERE channel=? AND start_date <= ? AND stop_date >= ?', [channel, now, now])
           row = cur.fetchone()
           if row:
              programming = program(row['channel'], row['title'], row['start_date'], row['stop_date'])
              cur.close()

这是 xbmc 日志:

- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <class 'sqlite3.InterfaceError'>
Error Contents: Error binding parameter 0 - probably unsupported type.
Traceback (most recent call last):
File "C:\Users\user\AppData\Roaming\XBMC\addons\script.tvguide\test.py", line 1682, in onAction
cur.execute('SELECT * FROM programs WHERE channel=? AND start_date <= ? AND stop_date >= ?', [channel, now, now])
InterfaceError: Error binding parameter 0 - probably unsupported type.
-->End of Python script error report<--

【问题讨论】:

  • 那么channel的类型是什么?
  • @CL。没有channel。我想在 sqlite 数据库中找到频道。你能帮忙吗?
  • @CL。你有什么想法吗?
  • 你没有回答我的问题。 type(channel) 是什么?

标签: python sqlite xbmc


【解决方案1】:

代替

cur.execute('SELECT * FROM programs WHERE channel=? AND start_date <= ? AND stop_date >= ?', [channel, now, now])

试试这个:

inq='SELECT * FROM programs WHERE channel=' + str(channel) + ' AND ' start_date <=' + str(now) + ' AND stop_date >= ' + str(now)

cur.execute (inq)

【讨论】:

  • 这通常是个坏主意。根据channel 的值的来源,这可能会导致SQL Injection 攻击。相反,parameterized queries 是首选,因为 @MartijnPieters 已经这样做了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
相关资源
最近更新 更多