简体   繁体   中英

TypeError: expected a character buffer object while translating

I am working on a project that uses an sqlite3 database to store some of the data.

My search function uses the SQL statement: '''SELECT text FROM snippets WHERE title=?''', (whichName,) and as my code was, whichName came in as a dictionary, which garnered this error:

Traceback (most recent call last):
  File "snippets.py", line 93, in <module>
    main()
  File "snippets.py", line 24, in main
    get_value_from_name(response)
  File "snippets.py", line 58, in get_value_from_name
    (whichName,))
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

Thus, I figured I needed to pass it as a string, so I just did name = str(response) to convert it to a string, but here is where the problem began. It gave me this:

[u'TEST'] <--- What is returned by the conversion to a string
None      <--- What is returned by the search function

when I converted the dictionary to a string. The search function then returned None because it was being passed [u'TEST'] instead of TEST like it should have been. So, I added some translation code:

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

    name = str(response)

    name = name.translate(translation_table)

This is where my current problem is. It is returning the following error:

Traceback (most recent call last):
  File "snippets.py", line 93, in <module>
    main()
  File "snippets.py", line 20, in main
    name = name.translate(translation_table)
TypeError: expected a character buffer object

I looked at these questions:

but none are applicable to my issue (as far as I can see.)

Does anyone know what is causing the Type Error?

Thanks!

I solved this. I ran the code in Python 3.3.2+ and it works find (albeit a few minor tweaks to the translation table)

Sorry if I wasted anyone's time. I will mark this as answer as soon as I can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM