【问题标题】:sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported typesqlite3.InterfaceError:错误绑定参数 1 - 可能不支持的类型
【发布时间】:2012-10-18 10:27:50
【问题描述】:

我遇到了这个我无法解决的烦人错误。这是我的功能

def savePicture(pic):
try:
    connection=sqlite3.connect('/home/faris/Desktop/site/site.db')
    db=connection.cursor()
    print type(pic.user.profile_picture)
    db.execute('INSERT INTO pictures (picture_id, caption, created_time, picture_url, link, username,full_name,profile_picture) VALUES (?,?,?,?,?,?,?,?)',
        [
        pic.id,
        pic.caption,
        pic.created_time,
        pic.get_standard_resolution_url(),
        pic.link,
        pic.user.username,
        pic.user.full_name,
        pic.user.profile_picture
        ])
    connection.commit()
    connection.close()
except sqlite3.IntegrityError:
    print 'pic already exist'

这是我的表(Sqlite :D)

-- Describe PICTURES
CREATE TABLE "pictures" (
"picture_id" INTEGER PRIMARY KEY,
"caption" TEXT,
"created_time" TEXT,
"picture_url" TEXT,
"link" TEXT,
"username" TEXT,
"full_name" TEXT,
"profile_picture" TEXT
)

这是我遇到的错误,

<type 'str'>
Traceback (most recent call last):
File "/home/faris/Desktop/site/cron/pictures.py", line 15, in <module>
savePicture(picture)
File "/home/faris/Desktop/site/db.py", line 36, in savePicture
pic.user.profile_picture
sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.

如您所见,我已经打印了“pic.user.profile_picture”的类型,它返回了 str 我也尝试过使用这两个函数( unicode 和 str )只是为了确保它返回一个没有运气的字符串..

有什么想法吗?欢呼:D

【问题讨论】:

    标签: python sqlite


    【解决方案1】:

    好吧,那是愚蠢的大声笑

        pic.caption,
        pic.created_time,
    

    不是 TEXT 类型..但是错误消息说明了问题 来自 pic.user.profile_picture。 因此,如果您遇到此错误只需检查所有参数 :)

    阅读下面的评论:)

    【讨论】:

    • 其实提示在最后一行:Error binding parameter 1。参数1为pic.caption(pic.id为参数0,pic.created_time为参数2等)
    • 这花了我几个小时。是的。检查所有参数的类型!有价值的愚蠢简单的建议。
    【解决方案2】:

    解决该问题的最简单方法是将所有数据框列转换为 str,并应用 to_sql 方法。 df = df.applymap(str) 否则,您可以在将数据框保存到 SQLite 表之前更改与 SQLite 数据类型兼容的每一列的数据类型。 to_sql 方法的 dtype 参数对于在将数据插入 SQL 表时转换列的数据类型很有用。

    【讨论】:

      【解决方案3】:

      错误信息

      错误绑定参数 1 - 可能是不支持的类型

      报告传递给 cursor.execute 的值序列中位置 1* 的值是 not supported by Python's sqlite3 module 类型的实例。

      Python的sqlite3模块默认支持传递None、int、float、str、bytes,也知道如何适配datetime.date和datetime.datetime实例。

      要修复错误,请将报告位置的值转换为支持的类型。

      可以通过注册适配器和转换器来模拟在数据库中存储不受支持的类型,以将该类型的对象与受支持的类型之一进行转换**。这是用于处理date 和datetime 实例的机制。假设我们有一个 Fruit 类,我们希望能够在数据库中存储和检索它。

      class Fruit:
          def __init__(self, name):
              self.name = name
      
          def __repr__(self):
              return f'Fruit({self.name!r})'
      

      我们创建适配器和转换器函数,并将它们注册到 sqlite:

      def adapt_fruit(fruit):
          # We must return a supported type.
          return fruit.name
      
      def convert_fruit(value):
          # value will always be bytes.
          return Fruit(value.decode('utf-8'))
      
      sqlite3.register_adapter(Fruit, adapt_fruit)
      
      # The first argument is the type of column that will store "fruits".
      sqlite3.register_converter('fruit', convert_fruit)
      

      我们可以将detect_types 传递给连接并创建一个列类型为fruit 的表:

      apple = Fruit('Apple')
      banana = Fruit('Banana')
      
      with sqlite3.connect('so12952546.db', detect_types=sqlite3.PARSE_DECLTYPES) as conn:
          cur = conn.cursor()
          cur.execute("""DROP TABLE IF EXISTS tbl""")
          # Create a column with our "fruit" type.
          cur.execute("""CREATE TABLE tbl (name fruit)""")
          # Pass Fruit instances as values.
          cur.executemany("""INSERT INTO tbl (name) VALUES (?)""", [(apple,), (banana,)])
          conn.commit()
          cur.execute("""SELECT name FROM tbl""")
          # Fruit instances are returned.
          for row in cur:
              print(row)
      

      输出:

      (Fruit('Apple'),)
      (Fruit('Banana'),)
      

      该列在 Sqlite 中的类型为 fruit,但在这种情况下,它将与支持的存储类相关联:text。

      sqlite> .schema tbl
      CREATE TABLE tbl (name fruit);
      sqlite> SELECT name, typeof(name) FROM tbl;
      Apple|text
      Banana|text
      

      * 位置是使用从零开始的计数来计算的,所以位置一是 second 值

      ** 此示例未涵盖所有选项 - 请阅读模块文档了解所有详细信息。

      【讨论】:

        猜你喜欢
        • 2020-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多