vendredi 27 novembre 2015

Python SQLite3 - Loop to Populate First Column

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

Aucun commentaire:

Enregistrer un commentaire