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

where in each [1-5].html, i have a line:

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

Now, the problem is, with this exact setup, I am getting error:

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

As I have never done database before, so I tried with:

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

i.e. increasing no of field in VALUES, which is then giving error:

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.

Since I have never done database befor, I am following Python-sqlite3 - Auto-increment of not null primary key?

but now, I am lost and cant figure out the problem.

Community
  • 1
  • 1
BaRud
  • 3,055
  • 7
  • 41
  • 89

1 Answers1

0

When you use two parameter markers (?), you also need to specify two values in each element of the lists list. But in this application, you do not actually want to specifiy different values (or any value at all).

With two columns in the table, you either have to give values for all columns:

INSERT INTO Electronic VALUES (NULL, ?);

or specifiy the columns you want to use:

INSERT INTO Electronic(Econf) VALUES (?);
CL.
  • 173,858
  • 17
  • 217
  • 259