sqlite3中提供的错误绑定

时间:2017-03-26 12:06:09

标签: python-3.x sqlite

我正在尝试从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 bindingssqlite3 - Incorrect number of bindings supplied但是无法解决我的问题。

请帮助。

1 个答案:

答案 0 :(得分:1)

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

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

要插入一行,请改用execute()