6

I am developing this program and for some reason, I keep getting this error. Is this an Ubuntu caused error, or something in my code that it crashing my program?

I am accessing a database and trying to remove the following characters from output: and, via value.translate(dictionary_of_bad_characters). That's where I get my error.

def get_data(plu):          # Get the data needed
    global value            # Set the variable value to global

    conn = sqlite3.connect('plus.sqlite')   # Connect to the sqlite3 database

    cursor = conn.cursor()  # Set cursor as the cursor for plus.sqlite

    results = cursor.execute('SELECT value FROM plus WHERE plu=?', [plu])
    # Above code gets the data value for value with the PLU of plu

    value = results.fetchone()            # Get the results of value

    data = [row[0] for row in results.fetchall()]

    translation_table = dict.fromkeys(map(ord, '+-(),'), None)
    # Above code creates a table with the mapped characters

    value = value.translate(translation_table)
    # Above code removes any unnescessary characters from value

    response = {    'PLU':      plu,
                    'Value':    value
                    }       # Create the response variable

    value = int(value)      # Convert value to type integer

    cursor.close()          # Close the cursor
    conn.close()            # Close the connection

    return(value)           # Return to the program flow
  • Why was this question posted here? I'm sure this should be on Stack Overflow. – Blaszard Jun 4 '16 at 0:30
  • @Blaszard Yes, you are technically correct, now. However, it's over 3 years old at this point and when it was posted the policy regarding dev questions was far more relaxed, and wasn't considered OT at the time. – RPiAwesomeness Jun 5 '16 at 14:54
8

This error indicates that value is a tuple, and not a string as you might expect. This indicates a problem with your application.

Here the problem is that fetchone() returns a one-tuple. You should change from this line:

value = results.fetchone()

to this (notice the comma after value):

value, = results.fetchone()

or this (not recommended):

value = results.fetchone()[0]

But why is fetchone() returning a tuple instead of a string? Because you can SELECT multiple columns. Consider for example the following SQL statement:

SELECT a, b, c FROM my_table;

In this case, fetchone() will return a three-tuple.

Writing value, = fetchone() you are telling Python that you are expecting a one-tuple and you want that single item placed into value. If you were expecting the three-tuple, you'd have used column_a, column_b, column_c = resulsts.fetchone().

This is the reason why you should prefer value, over fetchone()[0].


Bonus tip: I noticed you are using Python 3. In this case, you can write:

translation_table = dict.fromkeys(b'+-(),', None)

Speeding up your code and making it more clean.

| improve this answer | |
  • Oops, sorry (see the edit, I included the part that uses this). I knew about the fact that str is tuple and all that yadda-yadda, I just was wondering how to fix it (aka, convert to a string so I can - which, btw, str = str(str) also raises an error, I tried) – RPiAwesomeness May 7 '13 at 18:06
  • @RPiAwesomeness: I've updated my answer saying why str(str) does not work. – Andrea Corbellini May 7 '13 at 18:14
  • Yeah, I know why str(str) doesn't work. I was just using that as a placeholder in my posts/comments. You can see my updated post with code. – RPiAwesomeness May 7 '13 at 18:14
  • @RPiAwesomeness: here you are – Andrea Corbellini May 7 '13 at 18:24
  • Thanks for the answer, I ended up figuring it out from the Python help and Google, but I used the same thing you did. Thanks! – RPiAwesomeness May 26 '13 at 0:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.