Note that there are some explanatory texts on larger screens.

plurals
  1. POPython insert variable in loop into SQLite database using SQLAlchemy
    primarykey
    data
    text
    <p>I am using SQLAlchemy with declarative base and Python 2.6.7 to insert data in a loop into an SQLite database. </p> <p>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:</p> <pre><code>overall_star_ratings = doc.findall("//div[@id='maincontent2']/div/table/tr[2]//td/img") count_stars = len(overall_star_ratings) </code></pre> <p>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.):</p> <pre><code>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') </code></pre> <p>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:</p> <pre><code>d = {} for x in range(1, count_stars+1): count = x-1 d["t{0}_star".format(x)] = overall_star_ratings[count].get('alt') </code></pre> <p>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:</p> <pre><code> key = "t{0}_star".format(x) value = d["t{0}_star".format(x)] row.key = value </code></pre> <p>I've also tried adding the following after the above loop is completed:</p> <pre><code>for key, value in d.items(): row.key = value </code></pre> <p>The problem is that it is not inserting anything. It appears that the problem is in the <em>row.key</em> part of the script, not in the <em>value</em>, but I am not certain of that. From all that I can see, the <em>keys</em> 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.</p> <p>Any suggestions would be greatly appreciated!</p> <p>Thanks,</p> <p>Greg</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload