2016-12-06 70 views
0
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的,我有一句台词:现在数据库python3尺寸误差

\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) 

增加无场的价值观,然后给错误:

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. 

因为我从来没有做过数据库befor,我下面Python-sqlite3 - Auto-increment of not null primary key?

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

回答

0

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

随着表中的两列,你要么必须给值的所有列:

INSERT INTO Electronic VALUES (NULL, ?); 

或specifiy您要使用的列:

INSERT INTO Electronic(Econf) VALUES (?);