0

With Python3 I am trying to read in a text file and populate the first column of an SQLite3 table. The for-loop and execute combination is providing errors and I don't quite know what's going wrong.

import sqlite3
WORDLIST = 'words.txt'   #list of words only

con = sqlite3.connect('dict.db')
cursor = con.cursor()
cursor.execute('DROP TABLE IF EXISTS dict')
cursor.execute('CREATE TABLE dict(word text, part text, definition text)')
con.commit()

with open(WORDLIST,'r') as dictionary:
    for each_word in dictionary:
        print(each_word)
        cursor.execute('INSERT INTO rainbow_table VALUES (?)',(each_word,))

The input file words.txt is pretty simple

Cat
Bruno
Movie
Truth

I haven't yet been able to correct the run-time error:

Traceback (most recent call last):
  File "C:/dictdb.py", line 12, in <module>
    cursor.execute('INSERT INTO dict_table VALUES (?)',(each_word,))
sqlite3.OperationalError: table dict_table has 3 columns but 1 values were      supplied

1 Answer 1

1

As the error says, the database is expecting 3 values to be inserted into the 3 columns that your table has since you did not specify which columns you wanted to insert into. According to this link, if you want to insert only one of the three values, you have to specify which column you want with the following syntax:

INSERT INTO dict_table (word_text) VALUES (?)
Sign up to request clarification or add additional context in comments.

1 Comment

@CL. Whoops, I totally derped on that. Thanks. I removed it from my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.