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 465957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:25:48+00:00 2026-05-12T23:25:48+00:00

Thank you for bobince in solving the first bugs! How can you use pg.escape_bytea

  • 0

Thank you for bobince in solving the first bugs!

How can you use pg.escape_bytea or pg.escape_string in the following?

#1 With both pg.escape_string and pg.escape_bytea

    con1.query(
            "INSERT INTO files (file, file_name) VALUES ('%s', '%s')" %
            (pg.escape_bytea(pg.espace_string(f.read())), pg.espace_string(pg.escape_bytea(f.name)))

I get the error

AttributeError: 'module' object has no attribute 'espace_string'

I tested the two escapes in the reverse order unsuccessfully too.

#2 Without pg.escape_string()

 con1.query(
                "INSERT INTO files (file, file_name) VALUES ('%s', '%s')" %
                (pg.escape_bytea(f.read()), pg.escape_bytea(f.name))
        )

I get

WARNING:  nonstandard use of \\ in a string literal
LINE 1: INSERT INTO files (file, file_name) VALUES ('%PDF-1.4\\012%\...
                                                    ^
HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
------------------------
-- Putting pdf files in 

I get the following error

# 3 With only pg.escape_string

------------------------
-- Putting pdf files in
------------------------
Traceback (most recent call last):
  File "<stdin>", line 30, in <module>
  File "<stdin>", line 27, in put_pdf_files_in
  File "/usr/lib/python2.6/dist-packages/pg.py", line 313, in query
    return self.db.query(qstr)
pg.ProgrammingError: ERROR:  invalid byte sequence for encoding "UTF8": 0xc7ec
HINT:  This error can also happen if the byte sequence does not match the encoding expected by the server, which is controlled by "client_encoding".
  • 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-12T23:25:48+00:00Added an answer on May 12, 2026 at 11:25 pm

    INSERT INTO files(‘binf’,’file_name’) VALUES(file,file_name)

    You’ve got the (...) sections the wrong way round, you’re trying to insert the columns (file, filename) into the string literals ('binf', 'file_name'). You’re also not actually inserting the contents of the variables binf and file_name into the query.

    The pg module’s query call does not support parameterisation. You would have to make the string yourself:

    con1.query(
        "INSERT INTO files (file, file_name) VALUES ('%s', '%s')" %
        (pg.escape_string(f.read()), pg.escape_string(f.name))
    )
    

    This is assuming f is a file object; I’m not sure where file is coming from in the code above or what .read(binf) is supposed to mean. If you are using a bytea column to hold your file data you must use escape_bytea instead of escape_string.

    Better than creating your own queries is letting pg do it for you with the insert method:

    con1.insert('files', file= f.read(), file_name= f.name)
    

    Alternatively, consider using the pgdb interface or one of the other DB-API-compliant interfaces that is not PostgreSQL-specific, if you ever want to consider running your app on a different database. DB-API gives you parameterisation in the execute method:

    cursor.execute(
        'INSERT INTO files (file, file_name) VALUES (%(content)s, %(name)s)', 
        {'content': f.read(), 'name': f.name }
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thank you for Denis who solves the first bug! How can you have two
Thank you for your interesting in my question. You can help me out from
First of all thank you for your time and sorry if some issues are
Thank you for looking at my issue, hopefully you can help me out as
Thank you for quack in pointing out the off-by-one! The following code is my
Thank you in advance to anyone who can move this question to a better
Thank you in advance for all your help. I'm trying to use google maps
Thank you in advance for any help you can give. I am implementing sprites
EDIT: Thank you @yoda and @morispaa. You are both right and @morispaa's solution works,
Thank you all ahead of time. This is my first time developing a jQuery

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.