Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7037279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:31:31+00:00 2026-05-28T01:31:31+00:00

I am using SQLAlchemy with declarative base and Python 2.6.7 to insert data in

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:31:32+00:00Added an answer on May 28, 2026 at 1:31 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using SQLAlchemy declarative base and I have a method that I want isolate
I'm using SQLalchemy for a Python project, and I want to have a tidy
I've been trying to map an object to a database using SQLAlchemy but have
I'm using the SQLAlchemy Python ORM in a Pylons project. I have a class
I am using SqlAlchemy, a python ORM library. And I used to access database
I have a Pylons app that I'm using SqlAlchemy declarative models for. In order
I have recently started using SQLALCHEMY to query a my-sql database. I want to
I'm using SQLAlchemy Migrate to keep track of database changes and I'm running into
I am developing a Python web app using sqlalchemy to communicate with mysql database.
The following totally incomplete snippet defines a basic SQLAlchemy relationship using declarative syntax... Base

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.