I have been learning to use sqlite3 with 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?
spot.execute("INSERT INTO Words VALUES(?,?)", word_count,word)
I'm thought I might be messing something up with 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
. 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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With