473,787 Members | 2,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Literal Escaped Octets

I am trying to convert raw binary data to data with escaped octets in
order to store it in a bytea field on postgresql server. I could do this
easily in c/c++ but I need to do it in python. I am not sure how to read
and evaluate the binary value of a byte in a long string when it is a non
printable ascii value in python. I read some ways to use unpack from the
struct module, but i really couldn't understand where that would help. I
looked at the MIMIEncode module but I don't know how to convert the object
to a string. Is there a module that will convert the data? It seems to me
that this question must have been answered a million times before but I
can't find anything.

See http://www.postgresql.org/docs/8.1/i...pe-binary.html
for a description of the problem domain.
Feb 6 '06
10 1884
On Wed, 08 Feb 2006 00:57:45 -0500, Steve Holden wrote:
Chason Hayes wrote:
On Tue, 07 Feb 2006 01:58:00 +0000, Steve Holden wrote:

Chason Hayes wrote:

On Mon, 06 Feb 2006 13:39:17 +0000, Steve Holden wrote:

[...]

>The URL you reference is discussing how you represent arbitrary values
>in string literals. If you already have the data in a Python string the
>best advise is to use a parameterized query - that way your Python DB
>API module will do the escaping for you!
>
>regards
> Steve
Thanks for the input. I tried that with a format string and a
dictionar y, but I still received a database error indicating illegal
string values. This error went away completely when I used a test file
consistin g only of text, but reproduced everytime with a true binary file.
If you can let me know where I am wrong or show me a code snippet with a
sql insert that contains a variable with raw binary data that works,
I would greatly appreciate it.
I tried and my experience was exactly the same, which made me think less
of PostgreSQL.

They don't seem to implement the SQL BLOB type properly, so it looks as
though that rebarbative syntax with all the backslashes is necessary. Sorry.

regards
Steve

with regards to escaping data parameters I have found that I have to
specifically add quotes to my strings for them to be understood by
pstgresql. For example

ifs=open("binar ydatafile","r")
binarydata=ifs. read()
stringdata=base 64.encodestring (binarydata)

#does not work
cursor.execute( "insert into binarytable values(%s)" % stringdata)

#need to do this first
newstringdata = "'" + stringdata + "'"

then the select statment works.
Is this expected behavior? Is there a better way of doing this?

thanks for any insight


Yes, parameterize your queries. I assume you are using psycopg or
something similar to create the database connection (i.e. I something
that expects the "%s" parameter style - there are other options, but we
needn't discuss them here).

The magic incantation you seek is:

cursor.execute( "insert into binarytable values(%s)", (stringdata, ))

Note that here there are TWO arguments to the .execute() method. The
first is a parameterized SQL statement, and the second is a tuple of
data items, one for each parameter mark in the SQL.

Using this technique all necessary quoting (and even data conversion
with a good database module) is performed inside the database driver,
meaning (among other things) that your program is no longer vulnerable
to the dreaded SQL injection errors.

This is the technique I was hoping would work with the bytea datatype,
but alas it doesn't. ISTM that PostgreSQL needs a bit of work there,
even though it is otherwise a very polished product.

regards
Steve


That was it. Thanks for your great help.

Chason

Feb 9 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2995
by: Justin Koivisto | last post by:
I am replacing a string in a text block that has a literal $ in it, and preg_replace is seeing it as a backreference. Here is what I am using: foreach($price_lists as $list) $x=preg_replace('/--PRICE-LIST--/',$list,$x,1); OK, so what this does it is takes each array element and replaces only the first occurrance of "--PRICE-LIST--" with it. I would have used str_replace, but I didn't think it should be necessary to create an array...
6
6657
by: Walter L. Preuninger II | last post by:
I need to convert escape sequences entered into my program to the actual code. For example, \r becomes 0x0d I have looked over the FAQ, and searched the web, with no results. Is there a function that can do this, or do I need to use predefined constants or a table of the values? Below is a sample program. When run with the input w\rx, I want to see the output::
21
3412
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square brackets are not regular expression syntax. Thanks,
11
1964
by: emailscotta | last post by:
Below I declared a basic object literal with 2 methods. The "doSomething" method is call from the "useDoSomething" method but the call is only sucessful if I use the "this" keyword or qualify the call with "SomeObj". Can someone describe why this is happening? var SomeObj = { doSomething : function() {
4
1554
by: Trev | last post by:
Hi everyone, Thanks to all who have helped with various issues in the past. I've come up with a new one though: I've run some html through a javascript converter; basically it takes the html and outputs the following: var str=''; str+= // whatever text I want document.write(str);
0
1291
by: terence.parker | last post by:
This should be simple, but i've looked and looked and it seems all anyone wants to do is get the percent-encoding or convert FROM utf8 octets. But I want the octets themselves. As in, I want to input Chinese (or anything, for that matter) and get out something along the lines of \x05\x50\x3a etc... It seems neither utf8_encode nor decide does this job, and neither
4
5112
by: -Lost | last post by:
For example: var newlines = 'a\n\nb\n\nc'; alert(newlines); Yet, if I get that *exact* same line from an XMLHttpRequest's responseText, it is always alerted as: a\n\nb\n\nc
12
1860
by: Torsten Bronger | last post by:
Hallöchen! I need some help with finding matches in a string that has some characters which are marked as escaped (in a separate list of indices). Escaped means that they must not be part of any match. My current approach is to look for matches in substrings with the escaped characters as boundaries between the substrings. However, then ^ and $ in the patterns are treated wrongly. (Although I use startpos and endpos parameters for...
10
3027
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
How can I use a quote as a literal so it does get confused as not a literal? Thanks! Bob
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.