python - SQLite 值错误 : parameters are of unsupported type

标签 python sql sqlite

我在我的程序中使用了这段代码:

def insert(self, table, params):
  keys = ', '.join(params.keys())
  values = params.values()

  query = f"INSERT INTO songs ({keys}) VALUES (?, ?);" 
  print(keys)
  print(query)
  print(values)

  self.cur.execute(query , values)
  self.conn.commit()

  return self.cur.lastrowid

哪个打印

name, filehash
INSERT INTO songs (name, filehash) VALUES (?, ?);
dict_values(['testaudio8.mp3', 
'BA614A989B7BCF44E38D00EEFFE96F2D8BD6677D'])

但返回错误:

self.cur.execute(query , values)
ValueError: parameters are of unsupported type

我想将 dict_values 中的值插入到问号中,但出现错误。有人可以帮忙吗?

最佳答案

你需要做的是

# skip a few connect code etc 
cur = con.cursor 

values = ("nameToInsert", "fileHash") # <-- Tuple 

cur.execute("INSERT INTO songs (name, filehash) VALUES (?, ?)", values)

此外,如果您只有一个值,则需要像这样传递它:

value = ("onlyValue", ) # see the , 

否则不行。

关于python - SQLite 值错误 : parameters are of unsupported type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66140957/

相关文章:

Python:定时器,如何在程序结束时停止线程?

python - 如何在matplotlib python上孵化broken_barh?

mysql - 仅将匹配的数据从 CSV 导入到 MySQL

android - 将 FK 值添加到现有表条目

c# - dotConnect 或 "sqlite.phxsoftware.com"用于将 Entity Framework 与 SQLite 结合使用

python - flask +Sqlite : Strings that are integers return as type int?

python - 如何在 Gurobi 中编写多目标函数?

sql - 查找电话号码是否存在于 2 列之间

sql - TimescaleDB - 计数器

python - 我可以将函数设置为默认参数吗?