簡體   English   中英

Python SQLite3-循環以填充第一列

[英]Python SQLite3 - Loop to Populate First Column

使用Python3,我試圖讀取一個文本文件並填充SQLite3表的第一列。 for循環和execute組合提供了錯誤,我不太了解出了什么問題。

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

輸入文件words.txt非常簡單

Cat
Bruno
Movie
Truth

我還無法糾正運行時錯誤:

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

如錯誤所述,數據庫期望將3個值插入到表的3列中,因為您沒有指定要插入的列。 根據此鏈接 ,如果您只想插入三個值之一,則必須使用以下語法指定所需的列:

INSERT INTO dict_table (word_text) VALUES (?)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM