数据库python3维度错误

时间:2016-12-06 10:54:28

标签: database python-3.x sqlite

c.execute('''CREATE TABLE IF NOT EXISTS Electronic
          (elem_no INTEGER PRIMARY KEY, Econf text)''')
lists = []
for i in range(1,5):
    filenm = str(i)+".html"
    print(filenm)
    vals = []
    with open(filenm, "r") as elfile:
        for line in elfile:
            mstr = "]Electron Configuration"
            if mstr in line:
                vals.insert(len(vals),"Hello")
                print(len(vals))
                lists.append(vals)
print(lists)
c.executemany("INSERT INTO Electronic VALUES(?)",lists)
conn.commit()

在每个[1-5] .html中,我有一行:

\grep "Electron Configuration" 1.html
   [186]Electron Configuration 1s^1

现在,问题是,通过这种确切的设置,我收到错误:

1.html
1
2.html
1
3.html
1
4.html
1
[['Hello'], ['Hello'], ['Hello'], ['Hello']]
Traceback (most recent call last):
  File "elem_parse.py", line 134, in <module>
    c.executemany("INSERT INTO Electronic VALUES(?)",lists)
sqlite3.OperationalError: table Electronic has 2 columns but 1 values were supplied

因为我之前从未做过数据库,所以我尝试了:

c.executemany("INSERT INTO Electronic VALUES(?,?)",lists)

即。 增加VALUES中的字段,然后给出错误:

1.html
1
2.html
1
3.html
1
4.html
1
[['Hello'], ['Hello'], ['Hello'], ['Hello']]
Traceback (most recent call last):
  File "elem_parse.py", line 134, in <module>
    c.executemany("INSERT INTO Electronic VALUES(?,?)",lists)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied.

由于我从未做过数据库,我正在关注Python-sqlite3 - Auto-increment of not null primary key?

但现在,我迷失了,无法弄清楚问题。

1 个答案:

答案 0 :(得分:0)

使用两个参数标记(?)时,还需要在lists列表的每个元素中指定两个值。但是在这个应用程序中,你实际上并不想指定不同的值(或任何值)。

在表格中有两列,您必须为所有列提供值:

INSERT INTO Electronic VALUES (NULL, ?);

或指定要使用的列:

INSERT INTO Electronic(Econf) VALUES (?);