2016-12-06 100 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 (?);