1

I want to execute this query in Python:

cur=self.con.execute("insert into %s(url_id,url_name,url_title) values(%d,'%s','%s')" % (table,url_i,url_n,url_t))

Here, I'm inserting url id, url name and url title in the columns url_id, url_name and url_title respectively. The id and url are getting inserted pretty fine but not the url_title. The url_title column is simply blank.

If we, just for fun, swap the positions of url_n and url_t then the problem persists! I thought the problem might be related to the url_title column itself but this little experiment proves the problem might be with url_t variable.

We get the value of url_t by extracting the actual url title of the particular webpage using BeautifulSoup and Urllib2. Here's the function if you are interested:

def title(self,soup):
    url_title=soup.title.string
    return url_title

This returns the url title which I want to insert in the database.

I created the table, in which we intend to insert the values, using Django models. Here's the models.py file:

class url_list(models.Model):
    url_id=models.IntegerField()            
    url_name=models.CharField(max_length=200)       
    url_title=models.CharField(max_length=200)
    def __unicode__(self):
         return self.url_name

So, what am I doing wrong?

4
  • What html are you parsing (provide the link or relevant html code)? Also, inspect the values of table,url_i,url_n,url_t variables before the execute() call. Commented Apr 7, 2014 at 18:45
  • It's a simple static HTML document that I've created myself. I've a set of documents like this and their respective urls and url titles are supposed to be stored in the Db, which is not happening. I've inspected all the values and they seem to be correct yet it's not getting inserted. That's the problem right there. Commented Apr 7, 2014 at 18:52
  • 1
    Why are you inserting with raw SQL rather than using the model layer? Commented Apr 7, 2014 at 18:54
  • Sorry but I'm a newbie in this. Didn't really get you sorry. Commented Apr 7, 2014 at 19:04

2 Answers 2

1

Instead of messing with cursors and raw SQL, use the database api:

from yourapp.models import url_list

new_url = url_list(url_id=url_i, url_name=url_n, url_title=url_t)
new_url.save()
Sign up to request clarification or add additional context in comments.

3 Comments

This may be correct but now the problem is that I can't import the models! It's giving 'No module named XYZ found'
show us your import statements. are you trying to import them inside your views.py ?
No I'm not. I'm trying to import the models in a common python file.
1

I found the solution. In case you are using Django, please refer to Burhan Khalid easy solution that solves everything:

from yourapp.models import url_list

new_url = url_list(url_id=url_i, url_name=url_n, url_title=url_t)
new_url.save()

BUT, if you are not working in models or Django (or Both) and involves a normal table, let me draw your attention to the fact that the url_t variable is neither a string nor a unicode variable. When I did this:

type(url_t)

I got the type of url_t and it returned:

<class 'bs4.something'>

Which is something we don't want.

We want:

<type 'unicode'>

To convert it to unicode, just do the following:

unicode(url_t)

Even after converting it to unicode, I noticed the problem persisted. Then, with a few more experiments, I did the following:

url_t=str(url_t)
url_t=url_t[1:]

Voila. Problem solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.