473,890 Members | 1,350 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with quoted strings while inserting into varchar field ofdatabase.

hello,
I finally got some code to push a pickled list into a database table.
but now the problem is technically complex although possible to solve.
the problem is that I can nicely pickle and store lists in a blob
field with the help of dumps() for picklling into a string and then
passing the string to the blob.
I am also able to get back the string safely and do a loads() to
unpickle the object.
but this only works when list contains numbers.
if there is a list such als
lst = ["a","b","c"] then after a dumpls I get a pickled object into a
string but when I try to insert this into the blob field it refuses to
get into the table.
there is an sql syntax error.
I further discovered that the string variable that contains the
pickled object contains a lot of single quots "'" and this is what is
probably preventing the sql insert from succedding. can some one
suggest how to work around this problem?
regards,
Krishnakant.
May 6 '07 #1
11 3679
I further discovered that the string variable that contains the
pickled object contains a lot of single quots "'" and this is what is
probably preventing the sql insert from succedding. can some one
suggest how to work around this problem?
Every serious database driver has a complete and solid SQL escaping
mechanism. This mechanism tipically involves putting placeholders in
your SQL strings and passing python data in a separate tuple or
dictionary. Kinda

cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )

instead of:

cur.execute("IN SERT INTO datatable (data) VALUES ('%s');" %
(pickled_data,) )

It is the driver responsibility to serialize the data (which usually
involves adding enclosing quotes and escape odd charaters such as
quotes themselves).

What database/driver are you using? PostgreSQL+psyc opg2 or any other
wrong one? ;) In eiither case, read the driver documentation and the
DBAPI documentation (http://www.python.org/dev/peps/pep-0249/) for
further details.

-- Daniele

May 6 '07 #2
On 6 May 2007 11:22:52 -0700, Daniele Varrazzo <da************ **@gmail.com
Every serious database driver has a complete and solid SQL escaping
mechanism. This mechanism tipically involves putting placeholders in
your SQL strings and passing python data in a separate tuple or
dictionary. Kinda

cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )
I will try doing that once I get back to the lab.
mean while I forgot to mention in my previous email that I use MySQLdb
for python-mysql connection.
I did not find any such reference to storing pickled objects in the API.

any Idea what could be done with the mysql python module I am using?
regards,
Krishnakant.
May 7 '07 #3
On 7 Mag, 08:55, "krishnakan t Mane" <researchb...@g mail.comwrote:
On 6 May 2007 11:22:52 -0700, Daniele Varrazzo <daniele.varra. ..@gmail.comEve ry serious database driver has a complete and solid SQL escaping
mechanism. This mechanism tipically involves putting placeholders in
your SQL strings and passing python data in a separate tuple or
dictionary. Kinda
cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )

I will try doing that once I get back to the lab.
mean while I forgot to mention in my previous email that I use MySQLdb
for python-mysql connection.
OK: MySQLdb implements the escaping mechanism i described. You can
find the documentation if you look for it harder.
I did not find any such reference to storing pickled objects in the API.
Storing pickled object is not different from storing anything else
into BLOB. You would have faced the same problem if you had to write
"O'Reilly" in a VARCHAR field.

-- Daniele

May 7 '07 #4
On Mo, 7.05.2007, 10:30, Daniele Varrazzo wrote:
On 7 Mag, 08:55, "krishnakan t Mane" <researchb...@g mail.comwrote:
>On 6 May 2007 11:22:52 -0700, Daniele Varrazzo
<daniele.varra ...@gmail.comEv ery serious database driver has a
complete and solid SQL escaping
mechanism. This mechanism tipically involves putting placeholders in
your SQL strings and passing python data in a separate tuple or
dictionary. Kinda
cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )

I will try doing that once I get back to the lab.
mean while I forgot to mention in my previous email that I use MySQLdb
for python-mysql connection.

OK: MySQLdb implements the escaping mechanism i described. You can
find the documentation if you look for it harder.
>I did not find any such reference to storing pickled objects in the API.

Storing pickled object is not different from storing anything else
into BLOB. You would have faced the same problem if you had to write
"O'Reilly" in a VARCHAR field.

-- Daniele

--
http://mail.python.org/mailman/listinfo/python-list

Why not use qmark parameter passing (PEP 249) ?

cur.execute("IN SERT INTO datatable (data) VALUES (?);" , (pickled_data,) )

Then the DB driver will take care for you.
May 7 '07 #5
On 7 Mag, 10:46, "Stefan Sonnenberg-Carstens"
<stefan.sonnenb ...@pythonmeist er.comwrote:
On Mo, 7.05.2007, 10:30, Daniele Varrazzo wrote:
On 7 Mag, 08:55, "krishnakan t Mane" <researchb...@g mail.comwrote:
On 6 May 2007 11:22:52 -0700, Daniele Varrazzo
<daniele.varra. ..@gmail.comEve ry serious database driver has a
complete and solid SQL escaping
mechanism. This mechanism tipically involves putting placeholders in
your SQL strings and passing python data in a separate tuple or
dictionary. Kinda
cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )
I will try doing that once I get back to the lab.
mean while I forgot to mention in my previous email that I use MySQLdb
for python-mysql connection.

Why not use qmark parameter passing (PEP 249) ?

cur.execute("IN SERT INTO datatable (data) VALUES (?);" , (pickled_data,) )

Then the DB driver will take care for you.
>>import MySQLdb
print MySQLdb.paramst yle
format

MySQLdb (as many other drivers) use format parameter passing. Not much
difference w.r.t. qmark, at least when passing positional parameters:
the placeholder is "%s" instead of "?". A difference is that "format"
also allows named parameters (actually it should have been "pyformat",
but IIRC MySQLdb can also use named placeholders, even if they
advertise "format").

Anyway it is only a matter of placeholder style: they both allow the
driver to take care of data escaping, the concept the OT didn't know
about.

-- Daniele

May 7 '07 #6
On Mo, 7.05.2007, 11:32, Daniele Varrazzo wrote:
On 7 Mag, 10:46, "Stefan Sonnenberg-Carstens"
<stefan.sonnenb ...@pythonmeist er.comwrote:
>On Mo, 7.05.2007, 10:30, Daniele Varrazzo wrote:
On 7 Mag, 08:55, "krishnakan t Mane" <researchb...@g mail.comwrote:
On 6 May 2007 11:22:52 -0700, Daniele Varrazzo
<daniele.varra ...@gmail.comEv ery serious database driver has a
complete and solid SQL escaping
mechanism. This mechanism tipically involves putting placeholders
in
your SQL strings and passing python data in a separate tuple or
dictionary. Kinda
cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )
>I will try doing that once I get back to the lab.
mean while I forgot to mention in my previous email that I use
MySQLdb
>for python-mysql connection.

Why not use qmark parameter passing (PEP 249) ?

cur.execute("I NSERT INTO datatable (data) VALUES (?);" ,
(pickled_data, ))

Then the DB driver will take care for you.
>>>import MySQLdb
print MySQLdb.paramst yle
format

MySQLdb (as many other drivers) use format parameter passing. Not much
difference w.r.t. qmark, at least when passing positional parameters:
the placeholder is "%s" instead of "?". A difference is that "format"
also allows named parameters (actually it should have been "pyformat",
but IIRC MySQLdb can also use named placeholders, even if they
advertise "format").

Anyway it is only a matter of placeholder style: they both allow the
driver to take care of data escaping, the concept the OT didn't know
about.

-- Daniele

--
http://mail.python.org/mailman/listinfo/python-list

%s is not a placeholder IMHO.
What happens when using %s is, that the string given will be inserted where
%s is; that is something python does as with every print or such.
By using the qmark style, it is up the the implementation of the
cursor.execute method to decide what to do. python itself, and it's string
implementation, don't know anything to do with the qmark.
So, IMHO it *makes* a difference:
with %s the execute function sees a string and nothing more as the
parameters are consumed away by the % substitution.
with ?, the execute implementation must do it's best, it gets a string and
a list/tuple with values.

Cheers,
Stefan
May 7 '07 #7
cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )
%s is not a placeholder IMHO.
What happens when using %s is, that the string given will be inserted where
%s is; that is something python does as with every print or such.
It is indeed. The behavior you describe would be true if i had used
the "%" operator. Read better what i have written: There is no "%"
operator.

cur.execute() receives 2 parameters: a SQL string with placeholders
and a tuple with values: it's not me mangling values into the SQL
string. This is the driver responsibility and it has the chance
because it receives SQL and values as two distinct parameters. The
driver can ask the SQL string to contain placeholders either in qmark
"?" or in format "%s" style, but there is no functional difference.
Notice that the placeholder is always "%s" and not "%d" or "%f" for
integers or float: there is always an escaping phase converting each
python object into a properly encoded string and then the placeholders
are replaced with the value. This happens into the execute()
machinery.
By using the qmark style, it is up the the implementation of the
cursor.execute method to decide what to do. python itself, and it's string
implementation, don't know anything to do with the qmark.
So, IMHO it *makes* a difference:
with %s the execute function sees a string and nothing more as the
parameters are consumed away by the % substitution.
with ?, the execute implementation must do it's best, it gets a string and
a list/tuple with values.
Again, this would be true for "cur.execute(sq l % data)": what i wrote
is "cur.execute(sq l, data)".

-- Daniele

May 7 '07 #8
On Mo, 7.05.2007, 16:26, Daniele Varrazzo wrote:
cur.execute("IN SERT INTO datatable (data) VALUES (%s);",
(pickled_data,) )
>%s is not a placeholder IMHO.
>What happens when using %s is, that the string given will be inserted
where
%s is; that is something python does as with every print or such.

It is indeed. The behavior you describe would be true if i had used
the "%" operator. Read better what i have written: There is no "%"
operator.

cur.execute() receives 2 parameters: a SQL string with placeholders
and a tuple with values: it's not me mangling values into the SQL
string. This is the driver responsibility and it has the chance
because it receives SQL and values as two distinct parameters. The
driver can ask the SQL string to contain placeholders either in qmark
"?" or in format "%s" style, but there is no functional difference.
Notice that the placeholder is always "%s" and not "%d" or "%f" for
integers or float: there is always an escaping phase converting each
python object into a properly encoded string and then the placeholders
are replaced with the value. This happens into the execute()
machinery.
>By using the qmark style, it is up the the implementation of the
cursor.execu te method to decide what to do. python itself, and it's
string
implementation , don't know anything to do with the qmark.
So, IMHO it *makes* a difference:
with %s the execute function sees a string and nothing more as the
parameters are consumed away by the % substitution.
with ?, the execute implementation must do it's best, it gets a string
and
a list/tuple with values.

Again, this would be true for "cur.execute(sq l % data)": what i wrote
is "cur.execute(sq l, data)".

-- Daniele

--
http://mail.python.org/mailman/listinfo/python-list

Ashes on my head.

May 7 '07 #9
Ashes on my head.

My fault: the difference is hard to spot indeed in the rather long
line of the example. I should have been more explicit stating that the
differences were:

1. missing explicit quotes around the placeholders (they are part of
the escaped values),

2. no % operator: two parameters are passed instead.

Best regards,

-- Daniele

May 7 '07 #10

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

Similar topics

11
2074
by: bearophile | last post by:
Hello, here are a four more questions (or suggestions) for the language (probably people have already discussed some of/all such things: I've seen the contracts for Python: http://www.wayforward.net/pycontract/ http://www.python.org/peps/pep-0316.html They look interesting and nice, how Python developers feel about accepting something like this in the standard language? (Maybe they are a bit complex).
5
2145
by: Gary McCullough | last post by:
What I want to do sounds simple, but it's defeating me. I want to substitute all occurences of a colon : character in a string with an @ character -- unless the : occurs within a single or double-quoted substring. Surely this can be done with regular expressions? Any regex gurus know how to do it?
6
4806
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search string. It's actually the input string into a Microsoft Index Server search. The string will consist of words, perhaps enclosed in quotes or parentheses. I'd like to use Regex to pull out the words, or the phrases if the words are enclosed in quotes....
14
6129
by: dmh2000 | last post by:
I recently complained elsewhere that Python doesn't have multiline comments. i was told to use triple quoted strings to make multiline comments. My question is that since a triple quoted string is actually a language construct, does it use cause a runtime construction of a string which is then discarded, or is the runtime smart enough to see that it isn't used and so it doesn't construct it? example def fun(self):
4
2633
by: erikjalevik | last post by:
I have a long string of quoted strings, like: "string 1" "string 2" ... and I need to split this up into the constituent quoted strings. I was thinking it would be nice if I could somehow put it in an istringstream and use operator>> to parse quoted strings instead of whitespace-separated strings. Would it be at all possible to use a manipulator for this? So that I could write:
2
1677
by: donpro | last post by:
Hi, I have a varchar field in a MySQL database that contains a line of text like so: "This is a line if text" The double quotes are included in the database field. I cannot seem to display it on my HTML page, it always shows as blank.
13
8520
by: Vivek | last post by:
Hi, Is it possible to insert a '\0' value or for that matter any control character into a DB2 CHAR/VARCHAR field ? So if i do a 'select hex(column) from table' i should see a 0 in the output. How can i do it from the command prompt ? Thanks, Vivek
0
1291
by: Larrys | last post by:
I am fetching a VARCHAR field in a cursor. The VARCHAR field ins defined as 'not null with default'. When I am fetching a row where the VARCHAR field does not have a value, the value of the working storage text field that I am fetching INTO does not change. The value from the previous fetch remains. This is in a ZOS mainframe enviornment using a COBOL program. Thank You
8
4288
by: Lawrence D'Oliveiro | last post by:
If triple-quoted strings had the Python-nature, then they would take indentation into account. Thus: """this is a multi-line string.""" would be equivalent to
0
9823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10827
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...
0
10463
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
9637
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
8017
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
7170
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();...
1
4681
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3281
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.