当使用 pythons sqlite3 模块时,如果我要创建一个表并且第一行有 4 列,那么下一行必须有 4 列还是我可以有更多/更少?
我正在寻找创建词汇单词的数据库。每个词可能有不同数量的定义。
例如,“set”的定义要比“panacea”多得多。
我会用一个刮板来处理这个词汇数据库,它可以很容易地从字典参考网站上查找单词和定义。
#! /usr/bin/env python
import mechanize
from BeautifulSoup import BeautifulSoup
import sys
import sqlite3
def dictionary(word):
br = mechanize.Browser()
response = br.open('http://www.dictionary.reference.com')
br.select_form(nr=0)
br.form['q'] = word
br.submit()
definition = BeautifulSoup(br.response().read())
trans = definition.findAll('td',{'class':'td3n2'})
fin = [i.text for i in trans]
query = {}
for i in fin:
query[fin.index(i)] = i
## The code above is given a word to look up and creates a 'dict' of its definiton from the site.
connection = sqlite3.connect('vocab.db')
with connection:
spot = connection.cursor()
## This is where my uncertainty is. I'm not sure if I should iterate over the dict values and 'INSERT' for each definition or if there is a way to put them in all at once?
spot.execute("CREATE TABLE Words(Name TEXT, Definition TEXT)")
spot.execute("INSERT INTO Words VALUES(word, Definition (for each number of definitions))")
return query
print dictionary(sys.argv[1])
这不是作业,而是学习 sqlite3 的个人练习。