Gregory Saxton
Gregory Saxton

Reputation: 1311

Python insert variable in loop into SQLite database using SQLAlchemy

I am using SQLAlchemy with declarative base and Python 2.6.7 to insert data in a loop into an SQLite database.

As brief background, I have implemented a dictionary approach to creating a set of variables in a loop. What I am trying to do is scrape some data from a website, and have between 1 and 12 pieces of data in the following element:

overall_star_ratings = doc.findall("//div[@id='maincontent2']/div/table/tr[2]//td/img")
count_stars = len(overall_star_ratings)

In an empty SQLite database I have variables "t1_star,"..."t12_star," and I want to iterate over the list of values in "overall_star_ratings" and assign the values to the database variables, which varies depending on the page. I'm using SQLAlchemy, so (in highly inefficient language) what I'm looking to do is assign the values and insert into the DB as follows (I'm looping through 'rows' in the code, such that the 'row' command inserts the value for *t1_star* into the database column 't1_star', etc.):

if count==2:
    row.t1_star = overall_star_ratings[1].get('alt') 
    row.t2_star = overall_star_ratings[2].get('alt')         
elif count==1:
    row.t1_star = overall_star_ratings[1].get('alt') 

This works but is highly inefficient, so I implemented a "dictionary" approach to creating the variables, as I've seen in some "variable variables" questions on Stack Overflow. So, here is what I've tried:

d = {}
for x in range(1, count_stars+1):
    count = x-1 
    d["t{0}_star".format(x)] = overall_star_ratings[count].get('alt') 

This works for creating the 't1_star,' 't2_star" keys for the dictionary as well as the values. The problem comes when I try to insert the data into the database. I have tried adding the following to the above loop:

    key = "t{0}_star".format(x)
    value = d["t{0}_star".format(x)]
    row.key = value

I've also tried adding the following after the above loop is completed:

for key, value in d.items():
    row.key = value

The problem is that it is not inserting anything. It appears that the problem is in the row.key part of the script, not in the value, but I am not certain of that. From all that I can see, the keys are the same strings as I'm seeing when I do it the "inefficient" way (i.e., t1_star, etc.), so I'm not sure why this isn't working.

Any suggestions would be greatly appreciated!

Thanks,

Greg

Upvotes: 0

Views: 1132

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599600

Python attribute access doesn't work like that. row.key looks up the attribute with the literal name "key", not the value that's in the variable key.

You probably need to use setattr:

setattr(row, key, value)

Upvotes: 3

Related Questions