【问题标题】:incorrect binding supplied in sqlite3sqlite3 中提供的绑定不正确
【发布时间】:2017-08-19 02:26:45
【问题描述】:

我正在尝试从 cif 文件创建数据库,并收到一个奇怪的错误:

import sqlite3 as sq

sqlfile = "mats.db"
tabnm = "elems"
id_col = "Properties"
val_col = "Value"

conn = sq.connect(sqlfile)
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS Trial
          (Properties text, Name text, Phase text, AbM text, AbB text, Cpre text)''')
lists = []
filename = "/var/tmp/1101142.cif"

fullparams=["_journal_coden_ASTM" , "_journal_issue", "_journal_name_full"
,"_journal_page_first","_journal_page_last","_journal_paper_doi"
,"_journal_volume","_journal_year","_chemical_formula_sum"
,"_chemical_formula_weight","_space_group_IT_number","_symmetry_cell_setting"
,"_symmetry_space_group_name_Hall","_symmetry_space_group_name_H-M ","_atom_sites_solution_primary"
,"_atom_sites_solution_secondary ","_audit_creation_method","_cell_angle_alpha"
,"_cell_angle_beta","_cell_angle_gamma","_cell_formula_units_Z"
,"_cell_length_a","_cell_length_b","_cell_length_c"
,"_cell_measurement_reflns_used","_cell_measurement_temperature","_cell_measurement_theta_max"
,"_cell_measurement_theta_min","_cell_volume"]

with open(filename, "r") as cif:
  for line in cif:
    for j in range(6):
      if line.startswith(fullparams[j]):
        # print(line)
        lists.append(line.split()[-1])

print(lists)

c.executemany("INSERT INTO Trial VALUES(?, ?, ?, ?, ?, ?)",lists)
conn.commit()

虽然我已确定数字相等,但我仍然收到错误:

python3 parse_cif.py
['CMATEX', '7', "Materials'", '1745', '1752', '10.1021/cm0513738']
Traceback (most recent call last):
  File "parse_cif.py", line 36, in <module>
    c.executemany("INSERT INTO Trial VALUES(?, ?, ?, ?, ?, ?)",lists)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 6, and there are 1 supplied.

我不知道它是如何计算提供值 1 的。我已经看到类似的问题已经问过:Python 3 SQLite3 - Incorrect number of bindings, sqlite3 - Incorrect number of bindings supplied 但无法解决我的问题。

请帮忙。

【问题讨论】:

    标签: python-3.x sqlite


    【解决方案1】:

    executemany() 多次执行相同的语句。所以它不需要一个参数列表,而是一个包含多个参数列表的列表,每次执行一个。

    在这种情况下,'CMATEX' 看起来像一个包含六个字符的列表,所以它执行得很好。但是下一个列表'7' 只包含一个值;这是你得到的错误。

    要插入单行,请改用execute()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-06
      • 2013-10-28
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2020-09-10
      • 2013-05-27
      相关资源
      最近更新 更多