[英]Python TypeError when inserting sqlite3 values?
I have been learning to use sqlite3 with python. 我一直在学习使用sqlite3和python。 Right now, I have a function that takes a word and looks up the definition of that word on the internet. 现在,我有一个功能,它会在互联网上查找该词的定义。 I then try to store the word in one table and the definitions into another table linked together by a foreign-key. 然后我尝试将该单词存储在一个表中,并将定义存储到另一个表中,该表通过外键链接在一起。
Like this: 像这样:
#! /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 = {}
word_count = 1
def_count = 1
for i in fin:
query[fin.index(i)] = i
con = sqlite3.connect('vocab.db')
with con:
spot = con.cursor()
spot.execute("SELECT * FROM Words")
rows = spot.fetchall()
for row in rows:
word_count += 1
spot.execute("INSERT INTO Words VALUES(?,?)", word_count,word)
spot.execute("SELECT * FROM Definitions")
rows = spot.fetchall()
for row in rows:
def_count += 1
for q in query:
spot.execute("INSERT INTO Definitions VALUES(?,?,?)", def_count,q,word_count)
return query
print dictionary(sys.argv[1])
When I run it: 当我运行它:
./database_trial.py 'pass'
This is the error message: 这是错误消息:
Traceback (most recent call last):
File "./database_trial.py", line 37, in <module>
print dictionary(sys.argv[1])
File "./database_trial.py", line 28, in dictionary
spot.execute("INSERT INTO Words VALUES(?,?)", word_count,word)
TypeError: function takes at most 2 arguments (3 given)
Inside the function I only have two arguments being passed to the 'Words' table but, the message says there are three? 在函数内部我只有两个参数被传递给'Words'表,但消息说有三个?
spot.execute("INSERT INTO Words VALUES(?,?)", word_count,word)
I'm thought I might be messing something up with sys.argv. 我以为我可能会搞乱sys.argv。 So I went through and change that line to: 所以我经历了将该行更改为:
spot.execute("INSERT INTO Words VALUES(?,?)", word_count, sys.argv[1])
I still got the same results with the error message? 我仍然得到与错误消息相同的结果?
spot.execute("INSERT INTO Words VALUES(?,?)", word_count,word)
There are three arguments being passed to the method spot.execute
: the SQL string, the variable word_count
, and the variable word
. 有三个参数传递给方法spot.execute
:SQL字符串,变量word_count
和可变word
。 I suspect the values to enter into the database need to be enclosed in a tuple to form a single argument: 我怀疑进入数据库的值需要包含在一个元组中以形成一个参数:
spot.execute("INSERT INTO Words VALUES(?,?)", (word_count,word))
execute
takes only two parameters; execute
只需要两个参数; the second one is a tuple of all the values you want to use in the prepared SQL statement. 第二个是要在准备好的SQL语句中使用的所有值的元组 。 So, instead of 所以,而不是
spot.execute("INSERT INTO Words VALUES(?,?)", word_count,word)
wrap all values in a tuple: 将所有值包装在元组中:
spot.execute("INSERT INTO Words VALUES(?,?)", (word_count, word) )
# ^ ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.