FAQ
Hello all,

I am using python 2.6 and the psycopg2 module for the postgres connection
The following code is supposed to insert a record into a table with a bytea field. (bytearray)
I am having difficulty getting to field inserted properly. The snippet below inserts the first 8 bit hex value, which is 0xA5, correctly I believe - as \245
Not sure why, but the rest of the 'string' is not inserted into the db table.
Any ideas?

commandString = re.escape("A500046898000AC73D228B0B01800000000000000000000000000000000040003BF9")
insert = "INSERT INTO profile_commands(command_id, profile_id, execution_order, command, command_type) VALUES(%s, %s, 0, %s, 1); "
self.cur.execute(insert, (command_id, profileID,commandString,))
self.conn.commit()

Thanks for any ideas you can give me
Stacy

Search Discussions

  • MRAB at Mar 12, 2011 at 12:32 am

    On 11/03/2011 22:08, Meszaros, Stacy wrote:
    Hello all,

    I am using python 2.6 and the psycopg2 module for the postgres connection
    The following code is supposed to insert a record into a table with a bytea field. (bytearray)
    I am having difficulty getting to field inserted properly. The snippet below inserts the first 8 bit hex value, which is 0xA5, correctly I believe - as \245
    Not sure why, but the rest of the 'string' is not inserted into the db table.
    Any ideas?

    commandString = re.escape("A500046898000AC73D228B0B01800000000000000000000000000000000040003BF9")
    insert = "INSERT INTO profile_commands(command_id, profile_id, execution_order, command, command_type) VALUES(%s, %s, 0, %s, 1); "
    self.cur.execute(insert, (command_id, profileID,commandString,))
    self.conn.commit()

    Thanks for any ideas you can give me
    Stacy
    Does this help?

    http://stackoverflow.com/questions/3103242/inserting-text-string-with-hex-into-postgresql-as-a-bytea
  • Meszaros, Stacy at Mar 14, 2011 at 2:28 pm
    After much experimentation, I found the following solution:

    commandString = "\\xA500046898000AC73D228B0B01800000000000000000000000000000000040003BF9"
    insert = "INSERT INTO profile_commands(command_id, profile_id, execution_order, command, command_type) VALUES(%s, %s, 0, %s, 1); "
    self.cur.execute(insert, (command_id, profileID, commandString,))


    The 'key' to solving this was to add the \\x to the beginning of commandString.

    Thanks to all who pondered this

    Stacy
    ________________________________________
    From: MRAB [python at mrabarnett.plus.com]
    Sent: Friday, March 11, 2011 7:32 PM
    To: python-list at python.org
    Subject: Re: Inserting record into postgresql database
    On 11/03/2011 22:08, Meszaros, Stacy wrote:
    Hello all,

    I am using python 2.6 and the psycopg2 module for the postgres connection
    The following code is supposed to insert a record into a table with a bytea field. (bytearray)
    I am having difficulty getting to field inserted properly. The snippet below inserts the first 8 bit hex value, which is 0xA5, correctly I believe - as \245
    Not sure why, but the rest of the 'string' is not inserted into the db table.
    Any ideas?

    commandString = re.escape("A500046898000AC73D228B0B01800000000000000000000000000000000040003BF9")
    insert = "INSERT INTO profile_commands(command_id, profile_id, execution_order, command, command_type) VALUES(%s, %s, 0, %s, 1); "
    self.cur.execute(insert, (command_id, profileID,commandString,))
    self.conn.commit()

    Thanks for any ideas you can give me
    Stacy
    Does this help?

    http://stackoverflow.com/questions/3103242/inserting-text-string-with-hex-into-postgresql-as-a-bytea

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMar 11, '11 at 10:08p
activeMar 14, '11 at 2:28p
posts3
users2
websitepython.org

2 users in discussion

Meszaros, Stacy: 2 posts MRAB: 1 post

People

Translate

site design / logo © 2023 Grokbase