add-semi-colons
add-semi-colons

Reputation: 18810

Python: SQL Query and save it in dictionary (Bit Complicated data structure)

Based on couple of Api Calls I have constructed the following data structure (Dictionary). I have used python dictionary as a holding place.

325167 A,
953929 B, A, C
824420 D, B,
796992 K, M, Z,
825116 J, F, E,

I take the each value, and send a query to the database and get a results set select something from some_table where some_table.someCol = 'A'. Then I am trying to save this data in another dictionary to use it in future.

This is the error I am currently trying to deal, cl_to is the column name of the query that focusing.

Traceback (most recent call last):
  File "Category.py", line 148, in <module>
    categoryResults.append(categoryRow['cl_to']);
TypeError: 'NoneType' object is not subscriptable
{'cl_to': 'EP-plater_etter_\xc3\xa5r'}
{'cl_to': 'Musikk_i_2002'}
None

This is how my code looks like:

#Dictionary to save initial category with the rest of cl_to
baseCategoryTree = {}
categoryResults = []

# query get all the categories a category is linked to
categoryQuery = "select cl_to from sometable cl left join anotherTable p on cl.cl_from = p.another_column where p.some_column=14 and p.another_column ='";
cursor = db.cursor(cursors.SSDictCursor);

    for key, value in idTitleDictionary.iteritems():
        for startCategory in value[0]:
            #print startCategory + "End of Query";
            try:
                baseCategoryTree[startCategory] = [];
                #print categoryQuery + startCategory;
                cursor.execute(categoryQuery + startCategory + "'");
                done = False;
                while not done:
                    categoryRow = cursor.fetchone();
                    print categoryRow;
                    if not categoryRow:
                        done = True;
                        continue;
                categoryResults.append(categoryRow['cl_to']);
                baseCategoryTree[startCategory].append(categoryResults);
            except Exception, e:
                traceback.print_exc();

Best; N-H

Upvotes: 0

Views: 636

Answers (2)

enticedwanderer
enticedwanderer

Reputation: 4346

...

            while not done:
                categoryRow = cursor.fetchone();
                print categoryRow;
                if not categoryRow:
                    done = True;
                    continue;
            categoryResults.append(categoryRow['cl_to']);

...

continue will wrap to the while above it at which point categoryRow is going to be None.

Then when:

 categoryRow['cl_to'] 

is encountered... boom.

You probably want the following if you want to capture all rows:

...

            while not done:
                categoryRow = cursor.fetchone();
                print categoryRow;
                if not categoryRow:
                    done = True;
                    continue;
                categoryResults.append(categoryRow['cl_to']);

...

Upvotes: 1

Avaris
Avaris

Reputation: 36715

I think you meant categoryResults.append(categoryRow['cl_to']) in the while loop. Now, you are just looping until categoryRow is None and then try to append and get the obvious error.

By the way, get rid of all the ; at the end of lines. This is Python, not C/C++. ; is only required if you want two statements in a single line, like:

do_some_thing(); do_some_other_thing()

But this is generally a bad thing for readability. Use separate lines instead.

do_some_thing()
do_some_other_thing()

Upvotes: 1

Related Questions